PHP preg_replace

Discussion in 'PHP' started by cheatasp, Mar 20, 2008.

  1. #1
    Dear All,

    I have this problem, please help me.
    I want to replace www.google.com to http://www.google.com.com by using function preg_replace(), but i don't know how to do.


    Thanks
     
    cheatasp, Mar 20, 2008 IP
  2. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    
    $tring = 'hey search for stuff at www.google.com';
    $pat = '/www\.google\.com/';
    $replace = 'http://www.google.com';
    $new_string = preg_replace ($pat, $replace, $tring);
    
    echo $new_string;
    
    
    PHP:

     
    RoscoeT, Mar 20, 2008 IP
  3. cheatasp

    cheatasp Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks,

    what about if i want to change other word that not specific when user type the website with http://
    example: www.google.com,www.yahoo.com,......................


    Thanks
     
    cheatasp, Mar 20, 2008 IP
  4. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Do you mean you just want to add http:// onto whatever variable?

    for that just use the .= operator.

    $url = 'http://';
    $url .= 'www.google.com';

    The output of $url will now be http://google.com
     
    RoscoeT, Mar 21, 2008 IP
  5. cheatasp

    cheatasp Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes i mean like that, i mean that the variable depend on input if they type www.google.com it will convert to http://www.google.com, if www.yahoo.com it will convert to http://www.yahoo.com...........

    thanks
     
    cheatasp, Mar 21, 2008 IP