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
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.
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,
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
<?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:
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):