Jump to content

[SOLVED] Searching a string for URL


backyard

Recommended Posts

This probably isn't hard but I can't seem to find the function I need.

 

I have a string of say

 

$items = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa';

 

x,y,v,z,b and a can change value and the number of instances of each isn't fixed. I want to pull the complete string of http://www.website.com/bbb/id=1234. The id=1234 and the http://www.website.com will never change. Anyone know what function I can use to pluck out the url thus leaving $items='http://www.website.com/bbb/id=1234';

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/
Share on other sites

<?php
$data = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa';
preg_match_all('/((?:https?):\/\/(?:[a-z0-9]){3,3}?[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i', $data, $result, PREG_PATTERN_ORDER);
$result = $result[1];
print_r($result)
?>

 

works well for me

 

for a know website use

 

$data = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa ';
preg_match_all('%(http://www\.website\.com/[\S]*)%i', $data, $result, PREG_PATTERN_ORDER);
$result = $result[0];

print_r($result)

Hi lemmin,

 

not sure if it will work for you, but I use this function to extract urls from any text:

 

	// Convert www into links
function convert_URLS($content) {
	$content = eregi_replace("((ht|f)tp://www\.|www\.)([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})((/|\?)[a-z0-9~#%&\\/'_\+=:\?\.-]*)*)", "http://www.\\3", $content);
	$content = eregi_replace("((ht|f)tp://)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $content);
	return $content;
}

 

With a little work you should be able to get it to do what you want..

This works great !!

 

FYI : I have changed your URL to show you that this code works !

 

<?php
$string = "xxxyysdfsdfsdfsdfyvvvhttp://www.website.com/bbbbbbbbb/id=1234zzzaaa";
echo $string;
echo "<br>";
$str1=str_replace("http://","^",$string);
$pos1=strpos($str1,"^");
$str2=str_replace("com","*",$string);
$pos2=strpos($str2,"*");
$str3=str_replace("id","^",$string);
$pos3=strpos($str3,"^");
$str4=str_replace("1234","^",$string);
$pos4=strpos($str4,"^");
$site1=substr($string,$pos1,$pos2-($pos1-4));
$site2=substr($string,$pos3,$pos4-($pos3-4));
$finalsite=$site1.$site2;
echo $finalsite;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.