Preg_match_all from a url [duplicate] - php

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

Related

How do i get the part of the url in php [duplicate]

This question already has answers here:
PHP get the last 3 elements of an associative array while preserving the keys?
(4 answers)
Closed 4 years ago.
How do I get Last 3 parts of the url
For example , I have link like below
http://www.yoursite/one/two/three/drink.pdf
I will get the last part of the url using below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
But i need last 3 parts from the url like below
/two/three/drink.pdf
Please provide me a solution.
You could do this:
<?php
$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);
$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf
First, you use explode to break the url string into an array. Then use array_slice to get the last three elements of the array and finally implode to glue back the array elements using / as the glue.
Putting it all in one line would look like:
echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));
Try below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));
Hope this helps.

remove particular words from $string in php [duplicate]

This question already has an answer here:
How to replace multiple items from a text string in PHP? [duplicate]
(1 answer)
Closed 6 years ago.
I have the following string:
$string = java-developer-jobs
I would like to remove - and jobs from $string, but I don't want to use str_replace multiple times.
Use array as :
$toReplace = array('-','jobs');
$with = array('','');
$string = 'java-developer-jobs';
str_replace($toReplace,$with,$string);
You can use regex for this.Try something like this:
$string = "java-developer-jobs";
$sentence = preg_replace('/\-|jobs/', ' ', $string);
echo $sentence;
Output: java developer
LIVE DEMO

Extract lat and lon from a string [duplicate]

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;

How to keep only this specific part of url? [duplicate]

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]);

Extract a portion of a url

I am trying to extract a portion of a URL using regex.
An example of my url would be:
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=food
Using php, how could I extract the data in the q variable or the sourceid variable?
Don't use a regex for this. Instead, use parse_url() and parse_str().
$params = array();
$url= "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=food";
$url_query = parse_url($url, PHP_URL_QUERY);
parse_str($url_query, $params);
echo $params['q']; // Outputs food
Demo
A perfect tutorial for what you're trying to accomplish:
$parts = parse_url('http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=food');
parse_str($parts['query'], $query);
echo $query['q'];

Categories