Forum Moderators: coopster
if(preg_match_all('/(?:(http:\/\/|www\.)|http:\/\/www\.)(?:[^\.]+)?(?:\.[^\.]+)*?(\.?[^\.]+)\.com?(?:\.uk)?.*/is', $someVariable, $out)) {
echo 'Domains found:';
foreach($out[1] as $out) {
echo '<br />' . $out;
}
}
[edited by: Readie at 7:25 pm (utc) on Apr 18, 2010]
In another method. There are these urls
site . co. uk
site . com and so on
I need to search for the existence of "site" in the above url strings.
Can it be easily achievable?
return stristr($url, "site");
<?php
$urls = array("http://www.site.com/page", "http://site.com", "http://sub.example.com", "http://sub.site.co.uk","http://site.co.uk");
$exts =array("www.",".co.uk",".com",".net", ".org");
foreach($urls as $k=>$v){
$v = parse_url($v,PHP_URL_HOST);
$v = str_replace($exts,"",$v);
$u = explode(".",$v);
if(count($u)=== 1){
echo $u[0];
}else{
echo $u[1];
}
echo "<br />";
}
?>
if(count($u)=== 1){
echo $u[0];
}else{
echo $u[1];
}
echo end($u);
Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
[edited by: TheMadScientist at 1:16 pm (utc) on Apr 19, 2010]
<?php
$urls = array("http://www.site.com/page", "http://site.com", "http://sub.example.com", "http://sub.site.co.uk","http://site.co.uk");
$exts =array("www.",".co.uk",".com",".net", ".org");
$size = count($urls); // store so we dont evaluate count each time
for($i= 0; $i < $size; $i++){
$v = parse_url($urls[$i],PHP_URL_HOST);
$v = str_replace($exts,"",$v);
$u = explode(".",$v);
echo end($u);
echo "<br />";
}
?>