简体   繁体   中英

Using Preg_replace with Json

So I have attempted to get anything with a #texthere t change to a link so i can navigate them to the hash page of that specific hash.

With the code I have I just keep getting 'undefined' back. Can someone please take a look and point out where I am going wrong.

$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);

Okay this is what I've done to fix the issue. no decoding needed and that is what was causing the issue.

   $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'] ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM