将Preg_replace与Json一起使用

时间:2015-11-16 13:53:40

标签: php json preg-replace

所以我试图通过#texthere改变链接来获取任何内容,这样我就可以将它们导航到特定哈希的哈希页面。

根据我的代码,我只是不断定义'背部。有人可以看一看,指出我哪里出错了。

$json = array(
'userpost' => array()
);

$row = mysqli_fetch_assoc($check1);

        $posts = array();

         $posts['num'] = $num;
         $posts['streamitem_id'] = $row['streamitem_id'];
         $autoembed = new AutoEmbed();
         $posts['streamitem_content'] = $autoembed->parse($row['streamitem_content']);
         $regex = "/#(\w+)/"; 
         $string=$row['streamitem_content'];
         $string = preg_replace($regex, '<a href="hash.php?tag=$1">$1</a>', $string); 
         $posts['streamitem_content']=json_decode($string);
         $posts['streamitem_creator'] = $row['streamitem_creator'];
         $posts['streamitem_timestamp'] = $row['streamitem_timestamp'];
         $posts['username'] = $row['username'];
         $posts['id'] = $row['id'];
         $posts['first'] = $row['first'];
         $posts['middle'] = $row['middle'];
         $posts['last'] = $row['last'];

$json['userpost'][] =  $posts;

echo json_encode($json);

1 个答案:

答案 0 :(得分:0)

好的,这就是我为解决这个问题所做的工作。不需要解码,这就是导致问题的原因。

   $regex = "/#(\w+)/"; 
    $posts['streamitem_content'] = $row['streamitem_content'];
    $posts['streamitem_content'] = preg_replace($regex, "
    <a href='hash.php?tag=$1'>$1</a>",  $posts['streamitem_content'] );