1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Extract Text URL from String

Discussion in 'PHP' started by papa_face, Mar 28, 2009.

  1. #1
    Hello,

    I am trying to extract a text link from a given string however I am finding it rather difficult and I am getting no matches for some reason.

    My code is:
    <?php
      $string = "some random text http://tinyurl.com/dmugyw";
    function do_reg($text, $regex)
    {
       preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
       return $result = $result[0];
    }
    
    do_reg($string, '\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]');
    ?>
    Code (markup):
    Can anyone explain why I am not getting a match?
    I'm wanting to extract the actual text link into a variable so I can use it elsewhere in my script.

    Also can someone help me with this.

    I have a piece of text "#papa_face win http://www.somelink.com" or "#papa_face http://www.somelink.com"

    How can I extract each individual part of the text? If "win" is present in the text I want to run a particular function, but if its not present I want to run another. Is this possible? I don't want it to be to strict so this would also be acceptable "#papa_face win http://www.somelink.com" For this reason I cannot just explode() the string.

    Any help would be appreciated :)
     
    papa_face, Mar 28, 2009 IP
  2. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For the first matter:
    To get started, err on the side for simplicity. Use a simple, easier-to-read-and-debug regex for URL, and consider the slight-less efficient
    ereg()
    Code (markup):
    function instead.

    For the second matter:
    Use the PHP
    explode()
    Code (markup):
    function to break up the string, using the space character as delimiter. Then you can easily use array_has_key to find "win" if it exists.
     
    centralb, Mar 28, 2009 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    For the first problem, try using another regular expression. Here is a list of tons of them:

    http://regexlib.com/Search.aspx?k=url

    For the second, you can do this:

    
    $string = str_replace("\n",' ',$string);
    if(!strstr($string,' win ')){ echo 'win not found';}else{echo 'win found';}
    
    PHP:
    Make sure that the newline character is indeed \n.

    Peace,
     
    Barti1987, Mar 28, 2009 IP
  4. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #4
    I've tried http://regexlib.com/Search.aspx?k=url but I can't get any of them to work :(
    And good idea about the second bit, I'm gonna try it :)
     
    papa_face, Mar 28, 2009 IP
  5. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #5
    <?php
    $string = "some random text http://tinyurl.com/dmugyw";
    preg_match('/(http:\/\/)(.*)/', $string, $link);
    if (empty($link[0])) {
    	echo "Nothing found!";
    } else {
    	echo $link[0];
    }
    ?>
    PHP:
     
    ActiveFrost, Mar 28, 2009 IP
  6. CalebSpilchen

    CalebSpilchen Well-Known Member Affiliate Manager

    Messages:
    898
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    140
    Digital Goods:
    2
    #6
    Hi,

    try :
    
    $str = '<a href="http://foobar.com"> | Hello world Im a http://google.fr | Did you mean:http://google.fr/index.php?id=1&b=6#2310';
    $pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i';
    if (preg_match_all($pattern,$str,$matches)) {
    print_r($matches[1]);}
    Code (markup):
     
    CalebSpilchen, Dec 29, 2009 IP