http://example.com/codeThatIHave?killcode=codeIWant
How do I get the code I want using preg_match
tried this
preg_match_all("#killcode=([^=<]+)<#", $page, $del)
but not working
Try :
preg_match_all("#\?.+?=([^\s]+)#i",$page,$del)
I wouldn't use preg_match if I were you. PHP has a couple of useful functions that do exactly what you need/want: parse_url, which parses a url string into its various components, and parse_str, which breaks down a URI into an assoc array of key-value pairs:
$parsed = parse_url('http://example.com/codeThatIHave?killcode=codeIWant');//parse url into assoc array
parse_str($parsed['query'], $vars);//parse query string into assoc array
var_dump($vars);
var_dump($vars['killcode']);//get get param
Demo
Related
I don't know how to work with regex.
I get from file_get_contents (So is a string):
"sources": [{"file":"https:\/\/www40.playercdn.net\/98\/0\/Ya2t561cTjn95vvqwekm7w\/1475599977\/161003\/839RHzKW43tP1DAt.mp4","label":"360p","default":"true"},{"file":"https:\/\/www61.playercdn.net\/100\/1\/sRVvFxxRF2pk26D8D7C_1A\/1475599977\/161003\/950LRfeY1xeAfNQ3.mp4","label":"720p"}] ,"logo": {
And I need to extract only
https:\/\/www40.playercdn.net\/98\/0\/Ya2t561cTjn95vvqwekm7w\/1475599977\/161003\/839RHzKW43tP1DAt.mp4
and
https:\/\/www61.playercdn.net\/100\/1\/sRVvFxxRF2pk26D8D7C_1A\/1475599977\/161003\/950LRfeY1xeAfNQ3.mp4
And need to know that first URL is 360p and second 720p
Thank you
I would not use regex for this. Instead use json_decode to convert the data to an array. Json is basically string-based objects that can be easily transmitted over the internet.
$array = json_decode($data, true);
You can now use normal php array notation to get the values you want.
I have an array of URLs. The URLs look similar to:
https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=myAPIkey
The $place_id is a variable that changes in every single url.
I want to be able to select just this part of the url.
So when I loop through the URLs, I want it to extract that part, of the URL I am using, and assign it to a variable ($place) that I can use in my code. I have looked at strpos() and / or substr(), but I don't know how I would use them since the $place_id changes every time. I think the length of $place_id is always the same, but I am not 100% certain so a solution that worked for different lengths would be preferable.
Alternatively, I am already have the multiple $place_ids in an array. I am generating the array of URLs by using
foreach ($place_ids as $place_id) {
array_push(
$urls,
"https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=myAPIkey"
);
}
I am then using
foreach ($urls as $url){}
to loop through this array. If there was a way I could check to make sure the value of $place_id coincided with the value of the URL either through some sort of check or by using their position in the array that would work too.
I'm sorry if any of this is incorrect and if there are any questions I can answer or ways for me to improve my question I'd be happy to.
Since you're dealing with URLs here, you'll probably want to use PHP's built-in helpers, parse_url() and parse_str().
For example, you can separate the GET parameters with parse_url and then extract each argument into an array with parse_str:
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=<arg1>&key=<arg2>';
$args = [];
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $args);
// args => ["placeid" => "<arg1>", "key" => "<arg2>"]
A regular expression will do
$match = array();
$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=346758236546&key=myAPIkey";
preg_match('/[?&]placeid=([^&]+)(?:$|.*)/', $url, $match);
var_dump($match[1]); // string(12) "346758236546"
I have a string a[b][c] and I need to convert it into multidimensional PHP array. Does anybody knows a quick solution for that without using eval()?
Ok, I found solution for that:
parse_str("a[b][c]", $result);
Resulted multidimensional array is written into $result variable.
Source
So I would like to take a string like this,
q=Sugar Beet&qf=vegetables&range=time:[34-40]
and break it up into separate pieces that can be put into an associative array and sent to a Solr Server.
I want it to look like this
['q'] => ['Sugar Beets],
['qf'] => ['vegetables']
After using urlencode I get
q%3DSugar+Beet%26qf%3Dvegetables%26range%3Dtime%3A%5B34-40%5D
Now I was thinking I would make two separate arrays that would use preg_split() and take the information between the & and the = sign or the = and the & sign, but this leaves the problem of the final and first because they do not start with an & or end in an &.
After this, the plan was to take the two array and combine them with array_combine().
So, how could I do a preg_split that addresses the problem of the first and final entry of the string? Is this way of doing it going to be too demanding on the server? Thank you for any help.
PS: I am using Drupal ApacheSolr to do this, which is why I need to split these up. They need to be sent to an object that is going to build q and qf differently for instance.
You don't need a regular expression to parse query strings. PHP already has a built-in function that does exactly this. Use parse_str():
$str = 'q=Sugar Beet&qf=vegetables&range=time:[34-40]';
parse_str($str, $params);
print_r($params);
Produces the output:
Array
(
[q] => Sugar Beet
[qf] => vegetables
[range] => time:[34-40]
)
You could use the parse_url() function/.
also:
parse_str($_SERVER['QUERY_STRING'], $params);
Assuming I have the following URL on a webpage, how I can I utilize the explode() function to give me only the ID, and not the rest of the URL that follows the ID?
file.php?id=12345&foo=bar
I can get the ID, but there's always the following "&foo=bar".
Thanks.
If you need to treat a valid URL, you can use
parse_url
parse_url documentation
to explode the URL into different part (SCHEME, HOST, PORT, USER...)
and then, use
parse_str
parse_str documentation
on the QUERY part, in order to retrieve an array containing all your parameters.
Then, you can catch what you need.
parse_str($_SERVER['QUERY_STRING']);
echo $id;
parse_str will create PHP-variables from query-string variables.
http://www.php.net/manual/en/function.parse-str.php
If it's a full URL, you can use parse_url() to get the query string part.
If it's just a fragment, you can use explode() to get it, then parse_str():
list($path, $qs) = explode('?', $url, 2);
parse_str($qs, $args);
echo $args['id'];
The 2 tells explode() to break the string into a maximum of 2 parts (before and after the ?, in this case).