所以我使用 preg_match 来获取 # 之后的任何文本,直到字符串中的空格。但是,如果它在字符串中多次出现,它只会返回第一个。这是我目前所拥有的
$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;
输出 #demo1#demo1
当它应该输出前面带 # 的所有 3 个单词时。非常感谢任何帮助。
最佳答案
您想使用 preg_match_all
.
例子:
<?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;
}
关于php - preg_match foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6559876/