This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 7 years ago.
I have got the following string
http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384
I need to extract lat and lon from this string.
I got lat=51.529900 and lon=-0.785384 using the following code
$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";
list(,$lat,$lon) = explode("&",$str);
But then can't get the number.
Any help is highly appreciated. Thanks in advance.
You can use parse_str along with parse_url like as
parse_str(parse_url($str, PHP_URL_QUERY), $latlon);
echo $latlon['lat'];
echo $latlon['lon'];
$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";
$var = parse_str($str, $array);
$lat = $array['lat'];
$lon = $array['lon'];
Try this:
$str = "http://www.domain.com/map.aspx?isTrafficAlert=true&lat=51.529900&lon=-0.785384";
parse_str($str,$params);
print_r($params);
LAT: $params['lat'];
LON: $params['lon'];
Hope it help;
Related
This question already has answers here:
PHP - split String in Key/Value pairs
(5 answers)
Parse query string into an array
(12 answers)
Closed 1 year ago.
I have the following string:
$string = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
I'd like some PHP to grab the name and email address values from this string.
I'd originally tried the following but made no progress:
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
preg_match_all('#value=([^\s]+)#', $str, $matches);
echo implode(' ', $matches[1]);
I'd essentially like to say:
echo $string['No+Label+name']; // outputs name
Not a great e answer but you can use this if you have this string
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
$temp = explode('+', $str);
$name = explode('=',$temp[2]);
$email = explode('=',$temp[5]);
echo ($name[1]);
echo "<br/>";
echo ($email[1]);
This question already has answers here:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 7 years ago.
How can i get all the $_GET name => values from a url using preg_match_all
Example url1: http://www.example.com?go1=test1
Example url2: http://www.example.com?go2=test2
Example url3: http://www.example.com?go1=test1&go3=test3
The return need to be via array if is possible
$array = array();
$array[name1] = value1;
$array[name2] = value2;
...
How about something like:
$url = 'http://www.example.com?go1=test1&go3=test3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $array);
This will put the query string parameters in $array.
try to look $_SERVER['QUERY_STRING'] option,hopefully it will be helpful.
This may help you to get variables from url, you can also use parse_url to get a single Url components like
Using parse_url:
$url='http://www.example.com?go1=test1&go3=test3';
var_dump(parse_url($url)['query']);
See Demo :http://codepad.viper-7.com/AVFDdy
Using preg_match_all:
<?php
$re = "/\\?(.*)/";
$str = "http://www.example.com?go1=test1";
preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
See Regex :https://regex101.com/r/eB0rQ2/1
This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything
You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];
One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];
$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu
You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);
This question already has answers here:
how to split a comma seperated string into a two variables using php [closed]
(10 answers)
Closed 8 years ago.
Using PHP how can I split the
Szombathely, Hungary
into two variables
Szombathely
Hungary
Thank you!
array explode ( string $delimiter , string $string [, int $limit ] )
Source: http://php.net/manual/en/function.explode.php
$split = explode(',','Szombathely, Hungary');
That will, however, leave you with space before _Hungary
So if all are ,_ you could use
$split = explode(', ','Szombathely, Hungary');
or use trim on each of array elements.
There is a native command called explode, in which you pass as parameters the delimiter and also the string. More info here
In your case it would be:
$result = explode ( ',', 'Szombathely, Hungary')
as a result you get:
$result[0] = 'Szombathely'
$result[1] = 'Hungary'
Hope it helps!
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;
var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary`