Papaa Papaa - 1 month ago 8
PHP Question

Get the whole word that connects with '#'

I have this sentence

My name's #jeff,what is thy name?

And now i want to get
#jeff
from this sentence

I have tried this code

for ($i=0; $i <=substr_count($Text,'#'); $i++) {
$a=explode('#', $Text);
echo $a[$i];
}


But it returns
#jeff,what is thy name?
which is not what my soul desires

Answer

There is simpler solution to do it. Use preg_match() that find target part of string using regex.

preg_match("/#\w+/", $str, $matches);
echo $matches[0]

Check result of code in demo