1

I've written this code to link all hashtags in my blog posts:

function HashTags($string){
    global $post_userid;
    return preg_replace(
        '/\s*#([a-zA-Z0-9\-_]+)/i',
        " <a href=\"blog.php?id=".$post_userid."&search=$1\">#$1</a>",
        $string
    );
}

And it works awesome! but the problem is, that if there is some CSS code inside the same string, this function transform that css too..

Ex:

<div style="color: #fff">Hello world</div>

My question is: Is it possible to ignore that css with my regex function to avoid turning that #fff into a link too...

4
  • 2
    You can prepend eg <[^>]*>(*SKIP)(*F)| to skip <...> (demo) but generally a bad idea to parse html with regex. Commented Dec 8, 2019 at 19:48
  • thanks a lot! It worked awesome... can you please explain me why this is a bad idea? Commented Dec 9, 2019 at 3:02
  • @GuillermoEsquivelObregón Use < before a hash-tagged keyword to see why. In case of a user wants to use < in text and there is no limitation! Although if the text comes from a html editor or something the same, < may be converted to &lt; Commented Dec 9, 2019 at 10:21
  • @GuillermoEsquivelObregón Bad idea if not parsing your own html, but any arbitrary html where you don't know what to expect. Also See Using regular expressions to parse HTML: why not? Commented Dec 9, 2019 at 14:29

1 Answer 1

1

I have an idea,

  1. Remove all tags with strip_tags
  2. Search and find all hash-tagged keywords in resulted text
  3. Store found keywords in a list temporary
  4. Use a function to replace all keywords as you wish

Something like that:

function HashTags($string){
    global $post_userid;
    $tmp = strip_tags($string);
    preg_match_all('/\s*#[a-zA-Z0-9\-_]+/i', $tmp, $matches);
    foreach ($matches as $match) {
        $string = str_replace(
            $match,
            " <a href=\"blog.php?id=".$post_userid."&search=" . substr($match[0],1) . "\">$match[0]</a>",
            $string
        );
    }
    return $string;
}

It's not so clean but you can make it clean.

Sign up to request clarification or add additional context in comments.

2 Comments

tried but there still a problem with this solution: If an user want to post a #FFF hashtag and there is a div or another html tag with color: #FFF
@GuillermoEsquivelObregón So you may force some limitation on user input, because if there is no limitation, a user may choose to have <Tag color="#FFF"> in his (her) entered text and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.