我正在尝试使用preg_match_all
从字符串中提取主题标签,并使用foreach
循环对数组值执行MySQL插入。但显然它还没有工作,这就是我在这里的原因。
我的代码就像:
<?php
$text = "this has a #hashtag a #badhash-tag and a #goodhash_tag";
preg_match_all('/#[^\s]*/i', $text, $matches);
foreach($matches as $value){
echo $value.'<br>'; // right now, im just trying to see if i can get individual array values and then will have to perform sql query here
}
?>
我知道这有点天真,但我无法弄清楚。请指导。
答案 0 :(得分:1)
使用此for循环,$matches
是多维数组
foreach($matches[0] as $value){
echo $value.'<br>'; // right now, im just trying to see if i can get individual array values and then will have to perform sql query here
}
工作示例http://codepad.viper-7.com/iEGvgh
输出:
#hashtag
#badhash-tag
#goodhash_tag
答案 1 :(得分:0)
试试这个:
$text = "this has a #hashtag a #badhash-tag and a #goodhash_tag";
preg_match_all('/#(?P<hash>\w+)/', $text, $matches);
foreach($matches['hash'] as $val){
echo $val;
echo "<br>";
}