Jump to content

Recommended Posts

Need some help extacting the first url from a string. Whether it would start with http or https. Thanks

 

Input

this is some text  http://yahoo.com/directory/file.php?id=1&blah=1 this is some texthttp://msn.com this is some text

this is some text https://google.com.

 

Output

http://yahoo.com/directory/file.php?id=1&blah=1

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/
Share on other sites

:confused:

 

http://www.phpfreaks.com/forums/index.php?topic=294975.0

 

Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this.

 

I've searched. I couldn't find anything. Thats not what my question was related to.

 

http://www.phpfreaks.com/forums/index.php?topic=294975.0

 

Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this.

Using that matching expression posted in the preg_replace, use it in preg_match and viola, you have the first url matched. Simple as that.

I tried that. Doesn't work.

:confused:

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1073850
Share on other sites

<?PHP
$input = 'this is some text  http://yahoo.com/directory/file.php?id=1&blah=1  this is some texthttp://msn.com this is some texthis is some text https://google.com.';
$pattern = '/((?:https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i';
if(preg_match($pattern, $input, $match)) {
	echo $match[0];
} else { 
	echo 'Not found!';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1073931
Share on other sites

ajfrojojo, that's not how preg_match works; please read the appropriate manual page to see why. There is, however, a very similarly named function which would help lots with what you're after; I'll leave you to discover that on your own after reading the link above.

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1074414
Share on other sites

$pattern = '/((?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i';	
if(preg_match_all($pattern, $string, $match)) {
$string=$match[0][0];
echo $string;
}

I came up with that using preg_match_all. It's almost what I want.

 

$string = "<a href="http://google.com">Google</a> this is some text http://yahoo.com this is some texthttp://msn.com";

 

If the string was what I have above, how do I make the pattern stop at a " or '. Otherwise the first string produced would be http://google.com">Google</a>. I just want it to be http://google.com. It only stops at the first space. I would like it to stop at the first space, the first ", or the first '.

 

So I would want the output of $string=$match[0][0] to be http://google.com. The output of $string=$match[0][1] to be http://yahoo.com, and so on.

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1074416
Share on other sites

$pattern = '/((?:https):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i';

if(preg_match_all($pattern, $string, $match)) {

$string=$match[0][0];

echo $string;

}

 

Input:

http://yahoo.com this is some text

Output

http://yahoo.com

 

The above is ok

 

Input

<a  href="http://yahoo.com">Yahoo</a>

Output

http://yahoo.com"

 

I want to remove that last quote.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1074926
Share on other sites

The second input cannot output with a double-quote since the regex in your code cannot, ever match a double-quote.

 

Please, write show us a sample script (like what you have already, but with $string(s) being defined) that I can run, which gives you that trailing double-quote character in the match.

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1075459
Share on other sites

I cannot reproduce your output, nor can I see in your code how on earth that text can get matched.

 

See a live running example of your code (behaving properly) -- http://codepad.viper-7.com/W32a4S

 

Are you sure you're not just doing something dumb like echoing the original string rather than what was matched?

Link to comment
https://forums.phpfreaks.com/topic/205133-extacting-url/#findComment-1075502
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.