Preg_match foreach

So, I use preg_match to get the text after # up to the place in the line. However, if there are several cases in a string, it will return only the first. This is what I still have

$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet as $ht){
echo $ht;
}

echo $ht;prints #demo1#demo1when it should print all 3 C # words in front. Any help is appreciated.

+5
source share
2 answers

You want to use preg_match_all.

Example:

<?php 

$text = '#demo1 #demo2 some text #blah2';
$check_hash = preg_match_all("/([#][a-zA-Z-0-9]+)/", $text, $hashtweet);
foreach ($hashtweet[1] as $ht){
  echo $ht;
}
+14
source
0
source

All Articles