alfii Posted March 9, 2012 Share Posted March 9, 2012 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> ?> Quote Link to comment https://forums.phpfreaks.com/topic/258582-preg_match_all-array-and-foreach-warning-noob-question/ Share on other sites More sharing options...
ManiacDan Posted March 9, 2012 Share Posted March 9, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/258582-preg_match_all-array-and-foreach-warning-noob-question/#findComment-1325497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.