If Domain Is Found Do Nothing Else Add Domain

Discussion in 'PHP' started by !Unreal, Feb 21, 2009.

  1. #1
    Right, a user submits a URL. But there is not always a domain on the front. I want to add the URL to this string.

    Ive done this, but it inevitably doesn't work. Could someone tell me what ive done wrong please :)

    
    <?php
    $url = 'something.html';
    $target_url = 'http://www.google.com';
    if(preg_match($target_url, $url, $new)) {
        if(count($new) > 0) {
            $url = $target_url.$url;
        }
    }
    echo $url;
    ?>
    
    PHP:

     
    !Unreal, Feb 21, 2009 IP
  2. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What are you using preg_match for? preg_match is for checking if a string matches a regular expression, which isn't how you're using it there. Tell me what you're trying to do with preg_match and I can suggest a substitute.
     
    Shoro, Feb 21, 2009 IP
  3. Lpe04

    Lpe04 Peon

    Messages:
    579
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I can't figure out what you are trying to do.
     
    Lpe04, Feb 21, 2009 IP
  4. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #4
    !Unreal, Feb 22, 2009 IP
  5. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #5
    How can you add something if you don't know what he's trying to submit ? I can submit something.html, you will add google.com in the front of it .. Why ? Maybe I mean yahoo.com/something.html .. :rolleyes:
     
    ActiveFrost, Feb 22, 2009 IP
  6. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #6
    $target_url has been defined before and is the domain we need to use. $url is the user submitted url.

    Sorry I didn't explain it well at all :p
     
    !Unreal, Feb 22, 2009 IP
  7. MC_delta_T

    MC_delta_T Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    try this

    <?php
    $url = 'something.html';
    $target_url = 'http://www.google.com';
    if(stripos($url, $target_url) === FALSE) {
            $url = $target_url.$url;
    }
    echo $url;
    ?>
    PHP:
     
    MC_delta_T, Feb 22, 2009 IP