Jump to content

preg_match_all array and foreach [warning: noob question]


alfii

Recommended Posts

hey guys!

 

Im trying to get hashtags out of a string.

The function works so far- but i cant transfer the insides of the preg_match_all array into the string.

 

A hint would be fine already.

 

Thanks in advance- and here is some code

 

 

<?php
//example string
$strcontent = "ima string... #wat #taggy #taggytag im in your stringz, stealing your charz!";
  
//find hashtags
preg_match_all("/(#\w+)/", $strcontent, $matches);
echo $matches;            //the output is just "array" -> why?
foreach ($matches as $match)
{
//  $tempmatch=$match[1]; #####like this?
        //hiding the hashtags via span
$strtemp="<br>ima span<br>" . $match . "<br>ima /span<br>" . $strcontent;
$strcontent = $strtemp;
echo $strcontent;
}
#echo $strcontent;
  
  # <span style="display:none;"></span>  
?>

1)  It echoes "Array" because you're trying to echo an array, when you need to be using print_r()

 

2)  preg_match_all makes a multi-dimensional array with an internal array for each group of matches.  Therefore...

 

3)  Your loop should be on $matches[1] which will contain all of the matches for your first capture group (the hashtags)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.