1

我正在尝试使用循环从字符串中提取主题标签,preg_match_all并使用数组值执行 MySQL 插入。foreach但显然它还没有工作,这就是我在这里的原因。

我的代码是这样的:

<?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
}

?>

我知道这有点天真,但我无法弄清楚。请指导。

4

2 回答 2

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
于 2013-03-05T06:38:15.507 回答
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>";
 }
于 2013-03-05T06:39:32.460 回答