preg_replace hash tag?

How can I find this hash tag #test in the string?

preg_replace("/\b#test\b/i", "Found", $string);

It has to be with the # symbol!

I dont… understand. You’ve already got a regex for finding the word “#test” within a string.

I’m as perplexed as @StarLion.

Do you have a string that fails to replace the hash tag #test with “Found” that you can give us?

Or can you be more specific as to why what you have isn’t working?

When I use:

preg_replace("/\b#test\b/i", "Found", $string);

It doesn’t replace anything!

When I remove # and use:

preg_replace("/\btest\b/i", "Found", $string);

It works!

But the problem is that the string could contain many test words and only one #test so it will replace all of them!

I need to somehow make it search for #test not test

What is the value of $string?

$string = "this is a test string and this is a #test hash tag";

    echo preg_replace("/\b#test\b/i", "Found", $string);

is not in the set of \w, so \b isnt going to work at the front end of your string the way you want it to.

Buddy could you compile the REGEX for me please?

Here is what I have


Matches the following:

test #test test
test.#test test
test. #test test

Does not match the following

test#test test
test. #testtest

Get an error:

preg_replace(): Delimiter must not be alphanumeric or backslash

I didn’t put in the delimiter, so you must use

preg_replace("/\B(#test)\b/i", "Found", $string);
1 Like

Is that second result a valid one, though?

No idea. That is up to him :smile:

WORKS great thanks cpradio

Thank yo all guys for tips your help really appreciate you support love you all see ya

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.