php + regex - extract parameter data from a string [duplicate] - php

Users can input URLs using a HTML form on my website, so they might enter something like this: http://www.example.com?test=123&random=abc, it can be anything. I need to extract the value of a certain query parameter, in this case 'test' (the value 123). Is there a way to do this?

You can use parse_url and parse_str like this:
$query = parse_url('http://www.example.com?test=123&random=abc', PHP_URL_QUERY);
parse_str($query, $params);
$test = $params['test'];
parse_url allows to split an URL in different parts (scheme, host, path, query, etc); here we use it to get only the query (test=123&random=abc). Then we can parse the query with parse_str.

I needed to check an url that was relative for our system so I couldn't use parse_str. For anyone who needs it:
$urlParts = null;
preg_match_all("~[\?&]([^&]+)=([^&]+)~", $url, $urlParts);

the hostname is optional but is required at least the question mark at the begin of parameter string:
$inputString = '?test=123&random=abc&usersList[]=1&usersList[]=2' ;
parse_str ( parse_url ( $inputString , PHP_URL_QUERY ) , $params );
print_r ( $params );

Related

How to check preg_match for complicated text?

I've already seen some posts about it, but my text is a bit complicated,
And I can not get it to work.
Part of my page:
otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}
What I tried:
preg_match("/otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=(.*)/", $data[$n], $output);
echo $output[1];
What I want to present:
Just the number after uid=*
If the string you receive is reliably formatted like your posted examples, where the uid= parameter is the first query parameter after ? and is strictly a numeric string, you can use preg_match() to extract it by matching with (\d+) (match digits) because whatever follows in the next query parameter won't begin with a digit.
$str = 'otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}';
preg_match('/\?uid=(\d+)/', $str, $output);
echo $output[1];
// Prints "35577"
In practice I would avoid this though. The best way to handle this is to treat it as the JSON stream it is, in combination with PHP's built-in URL handling methods parse_url() and parse_str().
That solution looks like:
// Note: I made this segment a valid JSON string...
$input_json = '{"otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}';
$decoded = json_decode($input_json, TRUE);
// Parse the URL and extract its query string
// PHP_URL_QUERY instructs it to get only the query string
// but if you ever need other segments that can be removed
$query = parse_url($decoded['otherurl'], PHP_URL_QUERY);
// Parse out the query string into array $parsed_params
$params = parse_str($query, $parsed_params);
// Get your uid.
echo $parsed_params['uid'];
// Prints 35577

php - return last 3 parts of string (url)

I need to get the last three parts of a string (url). No need to pull the url from the browser as I already grab it from the database.
So, the string looks like: www.mysite.com/uploads/09/03/myimage.png
The part I'd like to extract from the string is "09/03/myimage.png"
<?php
$url="www.mysite.com/uploads/09/03/myimage.png";
$values=explode("/",$url); // this will split url string to array based on "/"char
$length=sizeof($values); //calculated array length
$lastthreeStringsCombined=$values[$length-3].'/'.$values[$length-2].'/'.$values[$length-1]; // formed new string by combining last 3 array elements
echo $lastthreeStringsCombined;
?>
Try this:
$lastThreeParts = implode('/', array_slice(explode('/', $url), -3, 3, true));
I'm assuming you want to pull out just the path part and ignore query parameters or hash values in the URL.
I would suggest using parse_url() (documented here: http://php.net/manual/en/function.parse-url.php) to pull out the path.
$url = 'www.mysite.com/uploads/09/03/myimage.png';
$path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $path);
$output = array_slice($parts, -3, 3);
var_dump($output);
This will provide you with an array of the last 3 parts of the path and handles cases like query strings and hash values in the array, of course you will still need to do basic length validation to ensure this logic still holds.

how to parse this url?

if I have this url: node/95/pdf/1. How will I able to get the numeric/value 1? Tried the parse_url but gave me the wrong output.
PS: the value 1 is just an example, the id is dynamic depends on what the user click.
I would use sscanf
Untested example:
list($node_id, $pdf_id) = sscanf($url, "node/%d/pdf/%d");
$node_id contains the node id, $pdf_id contains the pdf id. According to your comment: Yes, you can output it with e.g. echo $pdf_id;.
If you need them both in an array, you can remove the list() method, doing it like this:
$ids = sscanf($url, "node/%d/pdf/%d");.
This returns an array with both node and pdf id in $ids.
Finally, if you just need the pdf id, you could do
$id = sscanf($url, "node/95/pdf/%d");.
I just showed how to fetch both because I assumed you may need both numbers from your url.
Edit
seeing all the other answers after posting my solution, I am wondering why everyone is solving this with multiple functions when there is a function available that does exactly what he needs: parsing a string according to a format. This also leads to less sql-injection prone code IMHO. And it doesn't break something when the url gets extended or query strings are appended.
Edit 2
list($node_id, $sub, $sub_id) = sscanf($url, "node/%d/%[^/]/%d"); will get you the "pdf" and it's id separate instead of "node/%d/%s/%d". This is because char / is also matched by %s. Using %[^/] matches everything except the forward slash.
You can do this:
$id = end(explode('/', 'node/95/pdf/1'));
Example:
$arr = explode('/', 'node/95/pdf/1');
$id = end($arr);
echo $id; // 1
$url = "node/95/pdf/1";
// Find the last occurence of a slash, get everything after that.
$id = substr($url, strrpos($url, "/") + 1 );
Try with:
$input = 'node/95/pdf/1';
$parts = explode('/', $input);
$output = (int) $parts[3];

Pulling out a numeric value from the end of an SEO-friendly URL

I need to take a unique parameter from url generally appendened as last word of the url.
For example, I need to take 2220193 and 2220136 from the following urls:
"http://www.break.com/index/strange-sea-creature-retreats-into-ocean-floor-2220193"
"http://www.break.com/index/bully-picks-fight-with-sleeping-inmate-2220136"
Generally I use str_replace to remove the static parameter "http://www.break.com/index/", but I don't know how to delete the textual url and get only the number.
$id = substr($url, strrpos($url, '-') + 1);
Use explode on the url, this returns a string array. The last value in the string array should contain what you are looking for.
Given that $_SERVER['PATH_INFO'] would contain a url of the given format, you could use.
$url = $_SERVER['PATH_INFO'];
$urlParts = explode($url, '-');
echo end($urlParts);
Probably You could use preg_match PHP function
$outarr = explode('-',$inputstr);
$outnum = $outarr[count($outarr);

Elegant solution to read and transform query string in php?

I have a query string such as this:
file.php?search=keyword+here&genre1=1&genre4=1&genre19=1&genre181&director=436&actor=347&search_rating=3
I need to extract all the genres mentioned in the string, in this case its
genre1, genre4, genre19 and genre18
and output them into a string such as
ge1_ge4_ge19_ge18
What would be a good solution for this?
If you want the parameters passed by query string to the currently executing script then you simply need:
$genres = preg_grep('!^genre!', array_keys($_GET));
$out = implode('_', $genres);
Here you're filtering out all the parameters that start with genre using preg_grep() and getting a list of parameter names using array_keys().
If you have a URL you need to parse then use this snippet:
$url = 'file.php?search=keyword+here&genre1=1&genre4=1&genre19=1&genre181&director=436&actor=347&search_rating=3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
$genres = preg_grep('!^genre!', array_keys($params));
echo implode('_', $genres);
The difference here is that you use parse_url() to extract the query string and parse_str() to parse the query string.
Output:
genre1_genre4_genre19_genre181
parse_str() with the optional $arr argument is specifically built for exploding a query string properly:
Parses str as if it were the query string passed via a URL and sets variables in the current scope.
It can even deal with array arguments.
http_build_query() can glue an array back together with a custom $arg_separator but to get the output specifically as you want it, you will have to manually iterate through the arguments to make the transformation.
You could explode on the '=' then join on '_'.

Categories