Hello,
Is it not true that, parse_url() extract all the properties of a url and save each property into array ?
By properties I talking about:
scheme
host
port
user
pass
path
query
fragment
I reckon, the array actually looks like this:
[“0″=>”scheme”,”1″=>”host”,”2″=>”port”,”3″=>”user”,”4″=>”pass”,”5″=>”path”,”6″->”query”,”7″=>”fragment”]
True or not ? Tell me this!
Anyway, I now adding all this array to $url_properties = array();
To echo each value of the array. Like this ofcourse …
““
<?php
$url = ‘http://user:[email protected]/path.php?query_string=value#fragment1’;
print_r(parse_url($url)); echo ‘<br>’;
echo ‘<br>’;
var_dump(parse_url($url)); echo ‘<br>’;
$url_properties = array();
$url_properties = parse_url($url);
echo $url_properties[0];
echo $url_properties[1];
echo $url_properties[2];
echo $url_properties[3];
echo $url_properties[4];
echo $url_properties[5];
echo $url_properties[6];
echo $url_properties[7];
?>
But I get error:
**Notice: Undefined offset: 0 in C:xampphtdocs.
Notice: Undefined offset: 1 in C:xampphtdocs.
Notice: Undefined offset: 2 in C:xampphtdocs.
Notice: Undefined offset: 3 in C:xampphtdocs.
Notice: Undefined offset: 4 in C:xampphtdocs.
Notice: Undefined offset: 5 in C:xampphtdocs.
Notice: Undefined offset: 6 in C:xampphtdocs.
Notice: Undefined offset: 7 in C:xampphtdocs.
Why is this ?
This working fine, though….
““
<?php
$url = ‘http://user:[email protected]/path.php?query_string=value#fragment1’;
print_r(parse_url($url)); echo ‘<br>’;
echo ‘<br>’;
var_dump(parse_url($url)); echo ‘<br>’;
$url_properties = array();
$url_properties = parse_url($url);
foreach($url_properties AS $property)
{
echo $property;
}
?>
I get echoed:
**httpdomain.comuserpassword/path.phpquery_string=valuefragment1
This is proof that the $url_properties array is not empty.
So why the error on my first code when trying to echo each of the value of this array ? Strange!
EDIT:
I have figured my error. The array is associative array and so this is working:
““
echo $url_properties[‘scheme’];
echo $url_properties[‘user’];
echo $url_properties[‘pass’];
echo $url_properties[‘host’];
echo $url_properties[‘port’];
echo $url_properties[‘path’];
echo $url_properties[‘query’];
echo $url_properties[‘fragment’];
But what few lines of code should I add so that even the following works and does not give undefined offset error ?
““
echo $url_properties[0];
echo $url_properties[1];
echo $url_properties[2];
echo $url_properties[3];
echo $url_properties[4];
echo $url_properties[5];
echo $url_properties[6];
echo $url_properties[7];