I have a hashtag system: (Note: $body is a variable that is a post that a user submits. The hashtags are in the posts.) I have tried to do this using regex but have found this method to be as equally efficient and a bit easier to follow.
<?php
$string = $body;
$htag = "#";
$arr = explode(" ", $string);
$arrc = count($arr);
$i = 0;
while($i < $arrc) {
if(substr($arr[$i], 0, 1) === $htag) {
$arr[$i] = "<a href = 'category.php?#=$arr[$i]'>".$arr[$i]."</a>";
}
$i++;
}
$string = implode(" ", $arr);
?>
Then, $string
is echoed later in the page.
My problem with this is that my method for linking the hashtag to the category page using the php array element. On this page I want to call the word that was "hashtaged" and use a mysql query to get posts that have the hashtags. However, when I call the $arr[$i]
, to be echoed, I get an error:
Undefined offset: 1 on the line in which I call this array element into another variable.
Is there any way I can complete this task in a better and more effective way?