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...

  • 2
    You can prepend eg `<[^>]*>(*SKIP)(*F)|` to skip `<`...`>` ([demo](https://regex101.com/r/cAe0jj/1)) but generally a bad idea to parse html with regex. – bobble bubble Dec 08 '19 at 19:48
  • thanks a lot! It worked awesome... can you please explain me why this is a bad idea? – Guillermo Esquivel Obregon Dec 09 '19 at 03: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 < – JalalJaberi Dec 09 '19 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?](https://stackoverflow.com/q/590747/5527985) – bobble bubble Dec 09 '19 at 14:29

1 Answers1

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.

JalalJaberi
  • 2,417
  • 8
  • 25
  • 41
  • 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 – Guillermo Esquivel Obregon Dec 09 '19 at 02:57
  • @GuillermoEsquivelObregón So you may force some limitation on user input, because if there is no limitation, a user may choose to have in his (her) entered text and so on. – JalalJaberi Dec 09 '19 at 10:18