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));
Related
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
)
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] =>
// )
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I'm new with json and web service. After using insert command, I do print_r($result) and got this data:
Array ( [error_code] => 0 [error_desc] => [result] => Array ( [error_code] => 999 [error_desc] => Format input 'date' is wrong ) )
I want to print the error_desc only which is show Format input 'date' is wrong.
I try to parsing it with PHP but I only got empty result. Here is my code:
if (is_array($result)) {
if ($result['error_code'] =='999') {
echo $result['error_desc'];
}
}
Please help. Thanks.
I already got the answer. I just know after print the error_code. Thanks.
This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 8 years ago.
why is this wrong?
[enclosure] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
[length] =>
[type] => image/jpeg
)
)
I want to get the url to get the image file
I wrote print_r($eachItem->enclosure['#attributes']->url) it doesn't work. Why?
That is not the correct way of getting the attribute value. Use ->attributes() method:
echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)
Or
// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];
Right & Quick Format
$eachItem->enclosure->attributes()->{'url'};
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.