preg_match url get parameter parsing - php

I am trying to extract the latitude and longitude of a google maps url.
Such an url could look like this:
$url = 'http://maps.google.com/?ie=UTF8&ll=39.811856,11.309322&spn=0,0.485802&t=h&z=12&layer=c&cbll=39.311856,11.519322&panoid=1hltSSOoqv5H1dVKZWFkaA&cbp=13,117.95,,1,16.71';
As you can see, there's several location sets in the url. The cbll variable seems to be the right one in my case.
This is what I came up with:
preg_match("~&cbll=(-?\d+\.?\d*),(-?\d+\.?\d*)~", $url, $matches);
The problem: The preg_match seems to match the first '&ll=' in the url, and not the cbll part. I get the "&ll=" part of the url as result.

It works for me, if I var_dump $matches, I see this
array(3) {
[0]=>
string(25) "&cbll=39.311856,11.519322"
[1]=>
string(9) "39.311856"
[2]=>
string(9) "11.519322"
}

parse_str(parse_url($url, PHP_URL_QUERY), $vars);
list($a, $b) = explode(",", $vars['cbll']);

Related

php5 copy sub-strings from string separated by space?

I am using php5 and have a script which will return IP addresses of the client.
Executing script using shell_exec() function. Now the output is like this: *192.168.10.40 192.168.10.41 *.
Now I need to store this in an array. I used preg_match() but it is not working.
Here is the code using preg_match() :
$test = shell_exec("/www/dhcp.sh");
$pattern='/([^ ]*) /';
preg_match($pattern, $test, $new);
preg_match() is returning 0;
Here is the one I used explode() :
$test = shell_exec("/www/dhcp.sh");
var_dump( explode(' ', $test ) );
I also used explode but I am getting the result as:
array(1) { [0]=> string(28) "192.168.10.40 192.168.10.41 " }
Can anyone tell me how can I split the string into an array?
Regards,
Sowmya
You can use explode to split your string:
explode(' ', '192.168.10.40 192.168.10.41'));
which gives you
array(2) {
[0]=>
string(13) "192.168.10.40"
[1]=>
string(13) "192.168.10.41"
}
http://php.net/manual/fr/function.explode.php

Using PHP explode to create an array of words from $_GET

I'm having trouble using explode() in php.
I want to make an array of strings from the $_GET super global array.
The url will be like:
example/myproject.php?keywords=this+is+an+example
I want an array of the keywords so it should be like this:
myArray(6) = { [0]=> string(4) "this"
[1]=> string(2) "is"
[2]=> string(2) "an"
[3]=> string(7) "example" }
Here's my code:
$stringVals = explode("+",($_GET['keywords']));
var_dump($stringVals);
Here's the output:
array(1) { [0]=> string(30) "this is an example of a string" }
An example that works:
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
var_dump($pieces);
The output of this:
array(6) { [0]=> string(6) "piece1" [1]=> string(6) "piece2" [2]=>
string(6) "piece3" [3]=> string(6) "piece4" [4]=> string(6) "piece5"
[5]=> string(6) "piece6" }
I want the words from $_GET like that..
The "+" sign you see is actually just an encoded space. Therefore, you can split it normally using a space.
explode(' ', $_GET['keywords']);
Make sure you sanitize it if you're going to put it in a database.
Actually you can simply use:
explode(" ", $_GET['string'])
The + sign in the url actually means a space, not plus :- )
It's because spaces aren't allowed in the urls (url cannot have whitespaces), so it's actually converted to a plus sign.
In a normal GET request, the + in the URL will be converted back to spaces by the web server, so you should be exploding on ' '.
$stringVars = explode(' ', $_GET['keywords']);
See https://stackoverflow.com/a/2678602/1331451 for an explanation of why that is the case.
$myarray = explode(" ", $_GET['keywords']);
var_dump($myArray);
How's that?
Don't use plus symbol because The "+" sign you see is actually just an encoded space. use comma in URL while passing values from one page to another page here is solution after sending them in URL using comma separated form :-
$myArray = explode(',', $_REQUEST['keywords']);
after this you can get your data as following
$myArray[0]=this;
$myArray[1]=is;
$myArray[2]=an;
$myArray[3]=example;
$url = 'example/myproject.php?keywords=this+is+an+example';
$x = parse_url($url);
$y = str_replace("keywords=", "", $x["query"]);
var_dump(explode("+", $y));
First parse the url, second remove keywords=, next explode what's left by + sign.

Get a list of domains from a table via regex

I have list of domains in table with more info and
<td>example1.com</td>
<td>example2.org</td>
<td>example3.com</td>
<td>example4.com</td>
I need get .com domains using a regex. I tried to use something like :
'<td>(.............).com'
But what can I write instead of dots? What do I need to use?
I need get the data between the tags: <td>domain.com</td> -> domain.com
'<td>([^<]+\.com)</td>'
- it's more better, but i need get without tags
<?php
$html = '<td>example1.com</td>
<td>example2.org</td>
<td>example3.com</td>
<td>example4.com</td>';
$matches = array();
preg_match_all('/<td>(.*?.com)<\/td>/i', $html, $matches);
var_dump($matches[1]);
prints:
array(3) {
[0]=>
string(12) "example1.com"
[1]=>
string(12) "example3.com"
[2]=>
string(12) "example4.com"
}
Something like that:
'<td>([^<]+\.com)</td>'
but you shouldn't use regular expressions to parse html.
You can use look aheads and look behinds if you want to capture something but make sure it's surrounded by something else. Here we're capturing .com only.
<?php
$html = '<td>example1.com</td>
<td>example2.org</td>
<td>example3.com</td>
<td>example4.com</td>';
$pattern = "!(?<=<td>).*\.com*(?=</td>)!";
preg_match_all($pattern,$html,$matches);
$urls = $matches[0];
print_r($urls);
?>
Output
Array
(
[0] => example1.com
[1] => example3.com
[2] => example4.com
)

Get particluar values from a string in PHP

My string is :
$mystring = "https://maps.google.com/?ll=37.0625,-95.677068&spn=37.188995,86.572266&t=m&z=4";
Now I want those values which I have marked as bold from the string. How can I achieve this with PHP?
Use the following functions parse_url and parse_str. You could do something like this the following
$url = "https://maps.google.com/?ll=**37.0625,-95.677068**&spn=**37.188995,86.572266**&t=m&z=4";
$parts = parse_url($url);
if(isset($parts['query'])) {
parse_str(urldecode($parts['query']), $result);
print_r($result)
}
use explode or parse_url or parse_str
you'll have to use $_GET['ll'] and $_GET['spn'] to repectively obtain 37.0625,-95.677068 and 37.188995,86.572266
Here regular expression will be most efficient to parse your required values.
$url = "https://maps.google.com/?ll=37.0625,-95.677068&spn=37.188995,86.572266&t=m&z=4";
$regex = "/[-]*\d+.\d*/";
preg_match_all($regex,$url,$out, PREG_PATTERN_ORDER);
// $out = array(1) { [0]=> array(4) { [0]=> string(7) "37.0625" [1]=> string(10) "-95.677068" [2]=> string(9) "37.188995" [3]=> string(9) "86.572266" } }

Why preg_match fails to get the result?

I have the below text displayed on the browser and trying to get the URL from the string.
string 1 = voice-to-text from #switzerland: http://bit.ly/lnpDC12D
When I try to use preg_match and trying to get the URL, but it fails
$urlstr = "";
preg_match('/\b((?#protocol)https?|ftp):\/\/((?#domain)[-A-Z0-9.]+)((?#file)\/[-A-Z0-9+&##\/%=~_|!:,.;]*)?((?#parameters)\?[A-Z0-9+&##\/%
=~_|!:,.;]*)?/i', $urlstr, $match);
echo $match[0];
I think #switzerland: has one more http// ... will it be problem ?
the above split works perfect for the below string,
voice-to-text: http://bit.ly/jDcXrZg
In this case I think parse_url will be better choice than regex based code. Something like this may work (assuming your URL always starts with http):
$str = "voice-to-text from #switzerland: http://bit.ly/lnpDC12D";
$pos = strrpos($str, "http://");
if ($pos>=0) {
var_dump(parse_url(substr($str, $pos)));
}
OUTPUT
array(3) {
["scheme"]=>
string(4) "http"
["host"]=>
string(6) "bit.ly"
["path"]=>
string(9) "/lnpDC12D"
}
As far as I understand your request, here is a way to do it :
$str = 'voice-to-text from <a href="search.twitter.com/…;: http://bit.ly/lnpDC12D';
preg_match("~(bit.ly/\S+)~", $str, $m);
print_r($m);
output:
Array
(
[0] => bit.ly/lnpDC12D
[1] => bit.ly/lnpDC12D
)

Categories