SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting Text into HTML

    I have the following code:
    PHP Code:
    $text eregi_replace("\  ","  ",$text);
    $text preg_replace("/(https?:\/\/|ftp:\/\/|mailto:|www\.)([a-z0-9\._@]+)/i","<a href=\"\\1\\2\">\\1\\2</a>",$text);
    $text preg_replace("/href=\"www\.(.*)\">(.*)<\/a>/Ui","href=\"http://www.\\1\">[url]www.\\1[/url]</a>",$text); 
    and what it does is take a normal text document and look for URLs and turn them into hyperlinks. However, I want it to be able to also translate ? = - into those links as well. So, I inserted those characters into the character section like:
    PHP Code:
    $text eregi_replace("\  ","&nbsp;&nbsp;",$text);
    $text preg_replace("/(https?:\/\/|ftp:\/\/|mailto:|www\.)([a-z0-9\._@]?=-+)/i","<a href=\"\\1\\2\">\\1\\2</a>",$text);
    $text preg_replace("/href=\"www\.(.*)\">(.*)<\/a>/Ui","href=\"http://www.\\1\">[url]www.\\1[/url]</a>",$text); 
    but it doesn't work. Any ideas? Any help is greatly appreciated.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work:

    $text = eregi_replace(" ","&nbsp;&nbsp;",$text);
    $text = preg_replace("/(https?://|ftp://|mailto:|www.)([-a-z0-9\._@\?=\+]+)/i","<a href=\"\1\2\">\1\2</a>",$text);
    $text = preg_replace("/href=\"www\.(.*)\">(.*)</a>/Ui","href=\"http://www.\\1\">www.\\1</a></a>",$text);

    You need to escape question marks and periods, and a dash has to go at the beginning. Also, they needed to be inside the braces.

    Jeez these forums make regex hard.
    Last edited by Anarchos; Jul 18, 2001 at 17:07.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchos,

    First of all, thanks for your reply.

    Second, I get a parse error on line 6. Line 6 reads:
    PHP Code:
    $text preg_replace("/href=\"www.(.*)\">(.*)</a>/Ui","href=\"http://www.\1\"><a href="http://www.\1" target="_blank">[url]www.\1[/url]</a></a>",$text); 
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got the parse error fixed, but now i get the following (You can see what it looks like here) Here is the PHP code for that section:
    PHP Code:
    $text preg_replace("/(https?://|[url]ftp://[/url]|mailto:|[url]www.[/url])([-a-z0-9\._@]\?=\+)/i","<a href=\"\1\2\">\1\2</a>",$text); 
    $text preg_replace("/href=\"www.(.*)\">(.*)</a>/Ui","href=\"http://www.\1\"><a href=\"http://www.\1\" target=\"_blank\">[url]www.\1[/url]</a></a>",$text); 
    Any ideas anybody?
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •