PHP - How do I get a parameter value from $_SERVER['HTTP_REFERER']? - php

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 &.

Related

How to do this PHP find-replace

I think I need to use the preg_replace function but not sure exactly how to type in the patterns I want to find and replace. Basically, I want to replace this:
: u"x"x",
with this:
: u"x'x",
x means that any characters can go there. But I don't know how to write the x in PHP.
Thank you!
Edit: basically, I want to replace that middle double-quote with a single-quote. And I'll be searching through a big JSON file to do it. Probably should have said this at the start.
You could use this regular expression:
$result = preg_replace('#(: u".*?)"(.*?")#', "$1'$2", $string);

How to parse a string using regex or another technique?

I need to parse the id from the following string:
https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2
I need to only return the following:
id1161503945
The string always begins with https://itunes.apple.com/ and ends with ?i=#####&uo=2
I tried string and replace with wildcards but that did not work.
Well, you can use this below regex. It is working. I have use preg_replace function.
$data = 'https://itunes.apple.com/us/album/24k-magic/id1161503945?i=1161504024&uo=2';
echo preg_replace("/(.*)\/(\w+)\?(.*)/","$2",$data);
Output is
id1161503945
Or You can use
preg_match("/(\/)(\w+)(\?)/",$data,$m);
echo $m[2];
Same output.
Hope it help you
If it's really always the last element (before query params) in the url, then you can use this simple regex:
'/id[^?]+/'
CAUTION: as pointed by #xhienne, this works only if you're sure that another id string doesn't appear anywhere before the searched part.
If it may happen, rather use:
'/id[\d]+/'
This way, it's safe with respect to a previous id string, but the searched id must be followed by digits only.

Add %20 between terms returned from database

I have a database of 2 word names and they have a space between them however in order for my api call to work I need a %20 between the first and second word.
Currently mysql_query returns Anas platyrhynchos and I need it to return Anas%20platyrhynchos
any quick solution?
You can use PHP urlencode. That should solve your problem.
you can use the replace() function to return the correct string from your query
replace(columnname,' ','%20')
Alternatively you can use the urlencode() function to do this in your PHP code
You can do this on the PHP side using urlencode
You will need to urlencode the returned result from the database. This will add %20 and other special URL characters I am assuming you need.
Use Replace function.
Select Replace(colName, ' ', '%20')

Regex, how to match this string?

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

PHP Regex help, getting part of a link

I'm trying to write a regex in php that in a line like
<a href="mypage.php?(some junk)&p=12345&(other junk)" other link stuff>Text</a>
and it will only return me "p=12345", or even "12345". Note that the (some junk)& and the &(otherjunk) may or may not be present.
Can I do this with one expression, or will I need more than one? I can't seem to work out how to do it in one, which is what I would like if at all possible. I'm also open to other methods of doing this, if you have a suggestion.
Thanks
Perhaps a better tactic over using a regular expressoin in this case is to use parse_url.
You can use that to get the query (what comes after the ? in your URL) and split on the '&' character and then the '=' to put things into a nice dictionary.
Use parse_url and parse_str:
$url = 'mypage.php?(some junk)&p=12345&(other junk)';
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $parsed_str);
echo $parsed_str['p'];

Categories