parse_url PHP_URL_QUERY get a l value [duplicate] - php

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 3 years ago.
with the URL
domain.com/index.php?option=component&ctrl=product&task=show&cid=5076
$parse = parse_url( $full_url, PHP_URL_QUERY);
echo $parse['ctrl']; // I get nothing
How can I get my values from keys (option, ctrl, task, and cid)?

You can use parse_str along with parse_url,
$full_url = "domain.com/index.php?option=component&ctrl=product&task=show&cid=5076";
parse_str(parse_url($full_url,PHP_URL_QUERY), $arr); // parse query string
print_r($arr);
Demo
Output
Array
(
[option] => component
[ctrl] => product
[task] => show
[cid] => 5076
)

Related

PHP preg_match for url [duplicate]

This question already has answers here:
Parsing domain from a URL
(19 answers)
Parsing URL query in PHP [duplicate]
(5 answers)
Closed 5 years ago.
$request = '/test.php?action=save&key=e7s2uHhKKtcM32cHKUKM&login=test';
$param = preg_match("#^test.php?action=save&key=(\w+)&login=(\w+)($)#", $request, $match);
print_r($param);
It's not working. How to fix?
You need to escape your preg control characters "." and "?"
You are matching ^test meaning the string should start with "test", while your string starts with "/test"
I highly recommend to use single ticks so you don't have to escape your escape characters "\".
$param = preg_match('#^/test\.php\?action=save&key=(\w+)&login=(\w+)($)#', $request, $match);
print_r($param); // 1
print_r($match); // Array (
// (
// [0] => /test.php?action=save&key=e7s2uHhKKtcM32cHKUKM&login=test
// [1] => e7s2uHhKKtcM32cHKUKM
// [2] => test
// [3] =>
// )

How to get more than one results with preg_match_all and file_get_contents [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
i'd like to get more than one result using the fuction mentioned in the title, so far i'm just able to get just an only value using preg_match. I'd like to get an array of all the results and then record them in my db.
That's my current code:
$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('|<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="|' , $cast , $castimg );
print_r($castimg);
When i print the results i'm just giving: Array ( [0] => Array ( [0] =>
I made sure cast contains what i want. I've tried already a lot of possibilities and i got nothing :(
Try this one. I hope you grabbing all image URL's
$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('#<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="#' , $cast , $castimg );
print_r($castimg[1]);
Output
Array ( [0] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/d9NkfCwczP0TjgrjpF94jF67SK8.jpg [1] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/jdRmHrG0TWXGhs4tO6TJNSoL25T.jpg [2] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/gzjmHOSM1vnwnXpU737tdu9YjOu.jpg [3] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/zYiS1KJKEoLstzFA3DV5gwszzSC.jpg [4] => https://image.tmdb.org/t/p/w66_and_h66_bestv2/vDuvdRoliXbjQrptk7GVs4Qj07S.jpg ......)
If you use
var_dump($castimg);
instead
print_r($castimg);
you will see much more clear.

How to request parameter values from URL in PHP? [duplicate]

This question already has answers here:
How to get the value after the hash in "somepage.php#name"?
(3 answers)
Closed 9 years ago.
http://localhost/mysite/#id=98b7ee047f0a4fd6530d2538f7f929cc&com=289916
I need to get values of 'id' and 'com'.
I have to put '#' symbol in front parameter, because I need the page without refreshing itself.
Try this :
$url = 'http://localhost/mysite/#id=98b7ee047f0a4fd6530d2538f7f929cc&com=289916';
echo "<pre>";
print_r(parse_url($url));
Output :
Array
(
[scheme] => http
[host] => localhost
[path] => /mysite/
[fragment] => id=98b7ee047f0a4fd6530d2538f7f929cc&com=289916
)
For getting the url you can use
$url = $_SERVER['REQUEST_URI'];
then you can apply the above method Quoted by Prasanth Bendra.
echo ""; print_r(parse_url($url));

Key value in PHP from URL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to explode URL parameter list string into paired [key] => [value] Array?
Quick question
I have a URL sting that I wish to output the Key value as an array eg
$url ="www.domain.com?test=1&test2=2&test3=3";
and wish to have the output as an array
key => value so I can call any of the keys
eg
array (
test => 1,
test1 => 2,
test2 => 3,
)
cant use explode &
Just thinking do i have to do a loop and match between & and = for the key
I would use parse_url() with parse_str():
$url = parse_url('www.domain.com?test=1&test2=2&test3=3');
parse_str($url['query'], $keyvalue);
var_dump($keyvalue);
$keyvalue should contain your desired array.

string to array with desired keys [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a string into list of arrays
I have a string code=AA&price=10&user_id=5.initially i exploded first with & and then by = but i getting.
array(
[0] =>code
[0] =>AA
[0] =>price
[0] =>10
[0] =>user_id
[0] =>5
)
My aim is to show
array(
[code] => AA
[price] => 10
[user_id] => 5
)
parse_str($str, $values);
var_dump($values);
You are parsing a URL encoded format, use the already existing parse_str to parse it.
$string = 'code=AA&price=10&user_id=5';
$params = array();
parse_str($string, $params);
print_r($params);
http://php.net/manual/en/function.parse-str.php
parse_str — Parses the string into variables
parse_str('code=AA&price=10&user_id=5', $outputArray);

Categories