Reputation: 29
I want to write a PHP script which will first detect URL's and see if they have sub dir or not, if they are simple URL like site.com then it would write 1 in one of the DB's table but if the URL is something like this site.com/images or site.com/images/files then it should'nt do the query..
EDIT: Answer by Mob it works but doesnt work if there are more than one url
$url = "http://lol.com";
$v = parse_url($url);
if (isset( $v['path']) && (!empty($v['path'])) && ($v['path'] != "/") ){
echo "yeah";
} else {
echo "nah";
}
Upvotes: 1
Views: 89
Reputation: 11106
Use parse_url
$url = "http://lol.com";
$v = parse_url($url);
if (isset( $v['path']) && (!empty($v['path'])) && ($v['path'] != "/") ){
echo "yeah";
} else {
echo "nah";
}
EDIT:
To parse multiple urls;
Here:
<?php
$arr = array("http://google.com",
"http://google.com/image/",
"http://flickr.com",
"http://flickr.com/image" );
foreach ($arr as $val){
echo $val." ". check($val)."\n";
}
function check ($url){
$v = parse_url($url);
if (isset( $v['path']) && (!empty($v['path'])) && ($v['path'] != "/") ){
return "true";
} else {
return "false";
}
}
?>
The output is :
http://google.com false
http://google.com/image/ true
http://flickr.com false
http://flickr.com/image true
Upvotes: 2