Hey, I'm putting together some community software and I want people to be able to format their blogs but only via Forum style formatting.

like using

[ b ] for bold [ / b ]

which is easy cause i can just do an eregi_replace() with < b > and < / b >

BUT I'm really confused on how I can make links safe. How do I extract the THE_URL from like

[ u r l ] THE_URL [ / u r l ]

?

I've looked into sscanf and stuff and can't seem to do it or get rid of the [ / u r l]. I suck at regular expressions so if anyone could give me some help on how I could extract that out (and batch search/format/replace for it in a long post/string) , please let me know.

Thanks :glare:

    Okey I'll give my pretty messsy soution for this but not a pro so can't come upwith anything better.

    If a user would post sth I would explode the post by , Then count the array to see how many url's there are in there.
    THen use an for luss to go trough each one of them and take out the urls.
    Script:

    $urls=explode("url",$post);
    $amount=count($urls);
    $offset=0;
    for($i=0; $i<$amount; $i++)
    {
        $start=strpos($post,"[url]",$offset);
        $offset=$start;
        $end=strpos($post,"[/url]",$offset);
        $length=$end-$start;
        $url=substr($post,$start+5,$length);
        $offset=$end;
    //*
    }
    

    *Now you got the URL in url all you have to do is replace with <a href= and with >$url</a> as you said yourself

    If anyone has a better solution please tell
    🙂

      the most common way is to use regular expressions

      
      $message = preg_replace('#\[b\](.*?)\[/b\]#is', "<b>\\1</b>", $message);
      
      
      
      

        Originally posted by Ceril
        Well. One of the ways:

        http://www.php.net/strip_tags/

        It strips tags, and allows you to specify tags that should not be stripped.

        he wants to replace, not strip.

          Originally posted by rehfeld
          he wants to replace, not strip.

          Of course, but it is an easy way to strip out all tags except the ones you wish to allow. But then for example the [url] tag and other special tags, you would have to replace things.

            perhaps you are new to the internet. haha jk.

            Its not that i cannot use strip tags, its that i shouldnt... certain people have mastered abusing html with javascript among other things and something like strip_tags() which is quite a legacy function, will not stop it.

            Could I get a bit of elaboration on the whole regexp thing? Get an example for the URL thing?

              Try this:

              $v = <<<TXT
              BUT I'm really confused on how I can make links safe. 
              How do I extract the THE_URL from like [ u r l ]http://www.your-link.com[ / u r l ]
              or like [ u r l ]http://www.your-other-link.com[ / u r l ]
              TXT
              ;
              
              $pattern = '´\[ u r l \]\s*((?:https?|ftp|file)://[-\w+&@#/%?=~|!:,.;]*[-\w+&@#/%=~_|])\s*\[ / u r l \]´';
              $replace = '<a href="\1">\1</a>';
              $v = preg_replace($pattern, $replace, $v);
              
              echo $v;

              Of course you will have to remove the spaces from the tag in the pattern.

                Write a Reply...