0

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?

zkanoca
  • 9,664
  • 9
  • 50
  • 94
  • You should consider `preg_split("\\b", $string)` instead of `explode(" ", $string);`. `\b` knows about word boundaries so the string: `"This is a #tag."` will give you `#tag` instead of `#tag.`. – Halcyon Apr 16 '14 at 13:46
  • Even if you weren't getting an error, I really don't think that `category.php?#=$arr[$i]` is going to do what you want it to. – Patrick Q Apr 16 '14 at 13:46
  • Patrick, Would there be a better solution for the hyperlink to accomplish this? – user3513120 Apr 16 '14 at 13:47
  • Sure, use `category.php?tag=$arr[$i]` instead (or basically anything except `#` as the parameter key). – Patrick Q Apr 16 '14 at 13:48
  • This fixes the issue of the # getting to the other page. But I am still having a problem with the actual tag getting returned in the title. Is there a solution for that? – user3513120 Apr 16 '14 at 13:52

1 Answers1

0

Okay, so this can be greatly simplified using some regex and PHP's preg_replace function. @Doge was on the right track, but in my tests, \b didn't quite give the results that I think you want.

Basically, you can replace almost all of what you have with

$newText = preg_replace("/#(\w+)/", "<a href='category.php?tag=$1'>#$1</a>", $text);

As in…

$text = "This is a #hashtag within some #awesometext.";

$newText = preg_replace("/#(\w+)/", "<a href='category.php?tag=$1'>#$1</a>", $text);

echo $newText;

The result of this would be…

This is a <a href='category.php?tag=hashtag'>#hashtag</a> within some <a href='category.php?tag=awesometext'>#awesometext</a>.

See it in action here

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
  • If I used this, what could I do to replace the implode function in order to echo the linked hashtag? (As you can probably tell, I am not very familiar with regex.) :( – user3513120 Apr 16 '14 at 16:21
  • @user3513120 Did you look at the linked example? Simply echo the result of the `preg_replace`. It will output the input text with the tagged words replaced with linked versions of those words. – Patrick Q Apr 16 '14 at 16:23
  • Sorry, my bad, I was trying to combine the original and this method. However, my major problem was with the page the htags link too. How would I take the info taken in this script and use it to find all the posts from my db on another page? – user3513120 Apr 16 '14 at 16:34
  • @user3513120 That's really another question entirely. As asked, this particular question is about turning the tagged words in a given text into links. If you find that my answer solves that problem, it should be marked as correct. Before you create a new question about looking up the posts based on the provided tag, I suggest you do some basic searching on topics related to MySQL queries with PHP parameters, as this something for which there are already many answers. – Patrick Q Apr 16 '14 at 16:42