#1
  1. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    113

    Thumbs up Want to append http:.// in url


    I have one long description string which have some URL included, Among it some url have http:// and some have not so I want to add http:// in those URL. And for that I have write below Regex but it's not working.. can anyone help me out what should I have to do?

    PHP Code:
    <?php

    $str 
    'test ad <a href="www.google.com">Google</a> test ad 
                        <a href=\'www.gmail.com\'>Gmail</a> test ad 
                        <a href="http://www.google.com">Google</a>'
    ;



    $output preg_replace('@(href="|href=\'[^http://])@','$1http://',$str);

    echo 
    $output;

    ?>
    Last edited by web_developer; July 27th, 2011 at 11:05 PM. Reason: Forget to PHP quote
    I will try my best !!!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,185
    Rep Power
    9398
    Generally one can produce better results if one actually learns the material as opposed to just guessing.
    PHP Code:
    preg_replace('#href=["\'](?![a-z]+://)#i''$0http://'$str

IMN logo majestic logo threadwatch logo seochat tools logo