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] =>
// )
Related
This question already has answers here:
PHP function to build query string from array
(4 answers)
Closed 7 months ago.
How can I convert this array to query string in PHP? (I'm new to PHP), I'm passing multiple values for the filter $merchantFilter
print_r($merchantFilter)
gives:
Array ( [0] => Diesel Monkeys [1] => Gas Monkeys )
I'm trying to construct the URL like:
&merchantFilter[]=Diesel+Monkeys&merchantFilter[]=Gas+Monkeys
The variable that constructs the query is currently:
$HREF .= "merchantFilter=".urlencode($merchantFilter)."&";
How can I convert this to construct the array?
You can use http_build_query:
$merchantFilter= Array ( 'Diesel Monkeys', 'Gas Monkeys' );
echo http_build_query(array('merchantFilter' => $merchantFilter));
Output:
merchantFilter%5B0%5D=Diesel+Monkeys&merchantFilter%5B1%5D=Gas+Monkeys
Note the result is already urlencoded so needs no further processing.
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:
Numeric Values and Special charectors in str_word_count function
(4 answers)
Closed 5 years ago.
I have a string "34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90" but when I run the code below the array comes back as null. The numbers linked with hyphens are a numerical word from my point of view and I would like to know which numerical words appear in the string 2 or more times. In this case the answer would be the numerical word 34_56_67_78_97.
$string="34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90"
$words = str_word_count($string, 1);
$frequency = array_count_values($words);
$result = array_filter($frequency, function ($x) { return $x >= 13; });
Found a link to the question here at stackoverflow:
Numeric Values and Special charectors in str_word_count function
So my code should look like this to make it work:
$str="34_56_67_78_97 34_56_67_78_97 23_45_56_67_89 34_56_77_88_96 45_56_66_78_88 56_67_67_78_90";
$somearray = str_word_count($str, 1, '1234567890:#&_');
print_r($somearray);
Print out of code:
Array
(
[0] => 34_56_67_78_97
[1] => 34_56_67_78_97
[2] => 23_45_56_67_89
[3] => 34_56_77_88_96
[4] => 45_56_66_78_88
[5] => 56_67_67_78_90
)
This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 8 years ago.
i'm trying to parse tags from a string like the following:
$string = "foo [cmd:tag1] bar [cmd:tag2] bla bla";
$pattern = "/\[cmd:(.+)\]/";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$rc = $matches[0];
foreach($rc as $tag)
{
print_r2($tag);
}
which will return:
Array
(
[0] => [cmd:tag1] bar [cmd:tag2]
[1] => 4
)
what is wrong in my syntax as i'm expecting the following result:
Array
(
[0] => [cmd:tag1]
[1] => [cmd:tag2]
)
thanks
\[cmd:(.+?)\]
or use
\[cmd:([^\]]*)\]
Make your quantifier * non greedy by putting ? ahead of it.
See demo.
https://regex101.com/r/fA6wE2/23
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));