Announcement

Collapse
No announcement yet.

Parsing URLs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing URLs

    How do I parse all URLs in some text so they become hyperlinks instead of just being text URLs

  • #2
    PHP Code:
    // HYPERLINKS  \[url="http://xxx"\]yyy\[/url\]
        
    $string preg_replace("#\\[url=&quot;(ftp://|https://|http://|mailto:)(.*)&quot;\\](.*)\\[/url\\]#U","<a href=\"\\\\1\\\\2\" target=_blank>\\\\3</a>",$string);
    // HYPERLINKS  \[url="xxx"\]yyy\[/url\]
        
    $string preg_replace("#\\[url=&quot;(.*)&quot;\\](.*)\\[/url\\]#U","<a href=\"http://\\\\1\" target=_blank>\\\\2</a>",$string);
    // HYPERLINKS  \[url=http://xxx\]yyy\[/url\]
        
    $string preg_replace("#\\[url=(\ftp://\https://|http://|mailto:)(.*)\\](.*)\\[/url\\]#U","<a href=\"\\\\1\\\\2\" target=_blank>\\\\3</a>",$string);
    // HYPERLINKS  \[url=xxx\]yyy\[/url\]
        
    $string preg_replace("#\\[url=(.*)\\](.*)\\[/url\\]#U","<a href=\"http://\\\\1\" target=_blank>\\\\2</a>",$string);
    // HYPERLINKS  \[url\]http://xxx\[/url\]
        
    $string preg_replace("#\\[url\\](ftp://|https://|http://|mailto:)(.*)\\[/url\\]#U","<a href=\"\\\\1\\\\2\" target=_blank>\\\\2</a>",$string);
    // HYPERLINKS  \[url\]xxx\[/url\]
        
    $string preg_replace("#\\[url\\](.*)\\[/url\\]#U","<a href=\"http://\\\\1\" target=_blank>\\\\1</a>",$string); 
    I think this is right. I had to double escape everything so that the php tags wouldn't jack it up.

    Comment


    • #3
      mark that only deals with having [url] tags... he is asking to parse all urls in text with or without custom tags...

      Of course, stevie, you can easily modify the preg_replace code mark has provided you with to do what you want

      I would suggest looking up preg and ereg functions in the php manual because they are some of the most useful parsing tools around.
      Last edited by Conscience; Thu 23 May '02, 2:11pm.
      Cons

      Comment


      • #4
        /me slaps self
        Sorry, forgot.

        I've answered this question before so I tried to search for those threads. I couldn't find them.

        Play with the above code for a while. Let me know if you need me to go looking for that other stuff again.

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...
        X