Thread: RegEx-problem

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Göteborg, Sweden
    Posts
    9
    Rep Power
    0
    I've written a function to format a string from a form before it is displayed on the web page. I use the function to fix html-characters and line-breaks, allow som UBB-like coding and make all words starting with http:// or ftp:// hyperlinks.

    The problem is that if there is any form of punctuation after the url, the script adds that to the hyperlink as well, which means that I end up with bad url:s. Here's the function:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function fixOutput($string, $addlinks) {
    $string = htmlentities($string);
    $string = str_replace('', '<B>', $string);
    $string = str_replace('
    ', '</B>', $string);
    $string = str_replace('', '<I>', $string);
    $string = str_replace('
    ', '</I>', $string);
    if ($addlinks == true) {
    $string = ereg_replace("((ftp://)|(http://))(([[:alnum:]]|[[unct:]])*)", "<A HREF="" TARGET="_BLANK"></A>", $string);
    }
    $string = nl2br($string);
    return $string;
    }
    [/code]

    How can I fix the regular expression in the ereg_replace-function so that the last . , : ; ! ? or whatever (Like: "The best PHP discussion forum is <A HREF="http://www.devshed.com.")" TARGET=_blank>www.devshed.com.")</A> doesn't get included in the hyperlink. Like: The best PHP discussion forum is www.devshed.com.

    Thanks


    ------------------

    ::cj.kihlbom:: cj@kihlbom.com ::
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    you need a '$' in your regexp, this specifies the end of the evaluated string

    i.e
    $string = ereg_replace(".$","",$string);
    will get shot of that trailing .

    ------------------
    http://back-end.org
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Göteborg, Sweden
    Posts
    9
    Rep Power
    0
    Yeah, I know that, but the way I did it didn't work. I added [^.,:;!?]$ att the end of the expression, but that didn't work. The script didn't turn the url into a hyperlink at all then. :-(

    Any other suggestions?


    ------------------

    ::cj.kihlbom:: cj@kihlbom.com ::
  6. #4
  7. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    if ($addlinks == true) {
    $string = ereg_replace("((ftp://)|(http://))(([[:alnum:]]|[[ unct:]])*)", "<A HREF="" TARGET="_BLANK"></A>", $string);
    }
    >>


    kihlbom,


    Just try something like this..

    $string = eregi_replace( "([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=]]*)", "<A HREF="1://23" TARGET="_BLANK">1://23</A>", $string);

    //it will replace all the URL's with link..

    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited August 10, 2000).]
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Göteborg, Sweden
    Posts
    9
    Rep Power
    0
    That seems to work great! Thanks a lot!



    ------------------

    ::cj.kihlbom:: cj@kihlbom.com ::

Similar Threads

  1. Replies: 5
    Last Post: February 2nd, 2005, 01:28 AM
  2. UTF-8 problem in 4.1.1a
    By mehran20 in forum MySQL Help
    Replies: 1
    Last Post: February 8th, 2004, 03:21 PM
  3. WinNT4 problem
    By sandipguha in forum Windows Help
    Replies: 0
    Last Post: January 27th, 2004, 03:06 AM
  4. weird java problem..please help
    By suwandy in forum Java Help
    Replies: 1
    Last Post: January 26th, 2004, 05:13 AM
  5. Session Variable problem :(
    By modesty in forum PHP Development
    Replies: 1
    Last Post: January 15th, 2004, 05:00 AM

IMN logo majestic logo threadwatch logo seochat tools logo