I am doing a simple tutorial which able to catch the keywords automatically, the code is as below:-
$content = "#abc i love you #def #you , and you?";
preg_match_all("/[\n\r\t]*\#(.+?)\s/s",$content, $tag_matches);
print_r($tag_matches);
output:-
Array ( [0] => Array ( [0] => #abc [1] => #def [2] => #you ) [1] => Array ( [0] => abc [1] => def [2] => you ) )
'#' symbol with words are the keywords
the output is correct, but if I insert any punctuation symbols beside the keyword, e.g: #you, , the output will become you, , may I know how do I filter punctuation symbols after keywords?
besides this, if I insert any keywords together just like #def#you, , the output is def#you, is anyone can help me to separate it/
Thanks All.
Try using a word boundary
\binstead of whitespace\s. That will stop the match when it reaches anything other than a word character (i.e.,[a-zA-Z0-9_]).Conceptually, that’s what you were trying to do anyway by putting whitespace there (i.e., denote end of word).