I have a URL string and would like to extract parts of the URL. I have been trying to do understand how to do it with regex but no luck.
http://www.example.com?id=example.id&v=other.variable
From the example above I would like to extract the id value ie. example.id
I'm assuming you're not referring to actual $_GET variables, but to a string containing a URL with a query string.
PHP has built-in functions to process those:
parse_url() to extract the query string from a URL
parse_str() to split the query string into its components
No need for regexp here, just use php built in function parse_url
$url = 'http://www.example.com?id=example.id&v=other.variable';
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
Related
I have the url http://domain.com/script.php?l=7&p=146#p146. I want to be able to get the number after p=, without the #. Also, the hash may not always be there, so sometimes it could turn out as script.php?l=7&p=146. I know it's something to do with the regex character +, but I'm not completely sure on how to use it. Can someone please create the regex and explain how it works?
No need for regular expressions here.
$query = parse_url("http://domain.com/script.php?l=7&p=146#p146", PHP_URL_QUERY);
parse_str($query, $params);
echo $params['p'];
parse_url can get you all the distinct elements of a URL. And parse_str takes a query string (that stuff you find between ? and an optional # in a URL) and figures out the different parameters for you. You could also omit the parameter $params to the function, then parse_str would define some variables for you (afterward you could find the result in $p). But I personally rather dislike using parse_str with this side effect.
If you want to read up some more: PHP documentation on parse_url and parse_str
Don't reinvent the wheel. Use a built-in function, such as parse_url to parse the URL.
Documentation and examples: http://php.net/manual/en/function.parse-url.php
String to pull from : http:\/\/c.ypcdn.com\/2\/c\/rtd?vrid=357c99c36bd7ed631eda2e43fc9e30f8&rid=283d465f-f63b-4b0d-90b0-be6c12ed7617&ptid=943aw4l8qj&ypid=11720135&lid=194823099&tl=6&lsrc=SP&dest=http%3A%2F%2FCleanation.com
RegEx I have used before: www\..*?\.\w{2,5}
However the above RegEx will only grab the URL if it has a "www". in it. If I take out the "www." of the RegEx it justs grabs the c.ypcdn.com. I want to grab the Cleanation.com at the end of the string.
Needs to be dynamic so it can grab any url that doesn't have a "www." out of that url.
why not use parse_url() and then parse_str() on the returned query index to get it?
edit: example:
$url= "http://c.ypcdn.com/2/c/rtd?vrid=357c99c36bd7ed631eda2e43fc9e30f8&rid=283d465f-f63b-4b0d-90b0-be6c12ed7617&ptid=943aw4l8qj&ypid=11720135&lid=194823099&tl=6&lsrc=SP&dest=http%3A%2F%2FCleanation.com";
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query,$params);
echo $params['dest'];
If this is always the dest parameter, you can grab it with something like:
"dest=https?%3A%2F%2F([^?&]+?)"
If its aways the last parameter, you can grab it with:
"dest=https?%3A%2F%2F(.+)$"
I need a PHP validation function for URL with Query string (parameters seperated with &). currently I've the following function for validating URLs
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?#)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
echo preg_match($pattern, $url);
This function correctly validates input like
google.com
www.google.com
http://google.com
http://www.google.com ...etc
But this won't validate the URL when it comes with parameters (Query string). for eg.
http://google.com/index.html?prod=gmail&act=inbox
I need a function that accepts both types of URL inputs. Please help. Thanks in advance.
A simple filter_var
if(filter_var($yoururl, FILTER_VALIDATE_URL))
{
echo 'Ok';
}
might do the trick, although there are problems with url not preceding the schema:
http://codepad.org/1HAdufMG
You can turn around the issue by placing an http:// in front of urls without it.
As suggested by #DaveRandom, you could do something like:
$parsed = parse_url($url);
if (!isset($parsed['scheme'])) $url = "http://$url";
before feeding the filter_var() function.
Overall it's still a simpler solution than some extra-complicated regex, though..
It also has these flags available:
FILTER_FLAG_PATH_REQUIRED FILTER_VALIDATE_URL Requires the URL to
contain a path part. FILTER_FLAG_QUERY_REQUIRED FILTER_VALIDATE_URL
Requires the URL to contain a query string.
http://php.net/manual/en/function.parse-url.php
Some might think this is not a 100% bullet-proof,
but you can give a try as a start
I have a string category=45&format=1 that I want to convert into a key=value array.
Does anyone know if there is a quick way of doing this without having to write a function that explode's the & and then the = * snore *
Since you're dealing with the URL query format: parse_str
parse_str('category=45&format=1', $array);
http://php.net/parse_str
If it's a query string or doesn't contain special characters you can use parse_str.
In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:
http://www.google.com/aclk?sa=l&ai=CPWNSJV30TK{snip}&num=2&sig=AGiWqtxY{snip}
&adurl=http://www.jumpfly.com&rct=j&q=adwords&cad=rja
My question is what is the proper way to extract the value of q?
Should I search for the position of q, then the position of the next &, and then take the substring between them? That seems a bit unprofessional since what if someday q is the final parameter in that query string and then there is no & afterwards.
Thank you.
parse_str(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY), $queries);
echo $queries['q'];
References:
http://php.net/parse_url
http://php.net/parse_str
Try these:
parse_url(): http://php.net/manual/en/function.parse-url.php.
parse_str(): http://www.php.net/manual/en/function.parse-str.php
You can use parse_url() for that. From there, split the query on &.