regex - How to get the words that start with '#' in string using php? -


this question has answer here:

i have sentence

"my name's #jeff,what thy name?" 

and want #jeff sentence. have tried code

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

but returns #jeff,what thy name? not soul desires

there simpler solution it. use preg_match() find target part of string using regex.

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

check result of code in demo

if want matches string, use preg_match_all() find matches.

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

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -