PHP capture GPS position elements with regex - php

Hi I have some input like so:
$string = "0°25'30"S, 91°7'W"
I want formulate a regex expression in php which captures the individual elements so that I end up with:
$position_array = array([0] => 0 [1] => 25 [2] => 30 [3] => S [4] => 91 [0] => 7 [0] => W)
Any help would be much appreciated.

Off of the top of my head, I would try this:
$array = array_filter(preg_split("/[^A-Za-z0-9]/",$string), function ($segment) {
return strlen(trim($segment));
});

Related

Explode array three times

I have a string and I would like to explode with three differents patterns. The string looks like to :
country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4
I would like to get the differents parts of this two strings. The two lines are separated by a /n, the dates are separated by : and the link associated to date are separated with a ->
At the beginning I explode by the line break
$var = explode("\n", $var);
but when I tried to explode again this string, I get an error : *preg_split() expects parameter 2 to be string, array given*
How can I get the different parts ?
Thanks in advance.
Ideone link
Instead of using preg_split, consider using preg_match. You can write it as one big regex.
<?php
// Implicit newline. Adding \n would make an empty spot in the array
$str = "country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4";
$arr = split("\n", $str);
for ($i = 0; $i < count($arr); $i++) {
preg_match("/^(\w+)\:(\d\d\/\d\d\/\d\d)->(\w+)\:(\d\d\/\d\d\/\d\d)->(\w+)/", $arr[$i], $matches);
print_r($matches);
}
?>
Output:
Array
(
[0] => country:00/00/00->link:00/00/00->link2
[1] => country
[2] => 00/00/00
[3] => link
[4] => 00/00/00
[5] => link2
)
Array
(
[0] => country2:00/00/00->link3:00/00/00->link4
[1] => country2
[2] => 00/00/00
[3] => link3
[4] => 00/00/00
[5] => link4
)
EDIT
In your comment, you're posting dates with 4 digits, whereas in your question, they only had 2 digits.
Therefore you need to change the regex to:
/^(\w+)\:(\d\d\/\d\d\/\d\d\d\d)->(\w+)\:(\d\d\/\d\d\/\d\d\d\d)->(\w+)/
How about using preg_match_all:
<?php
$data =<<<ENDDATA
country:00/00/00->link:00/00/00->link2
country2:00/00/00->link3:00/00/00->link4
ENDDATA;
preg_match_all('#(\d{2}/\d{2}/\d{2})->(.[^:\n]+)#', $data, $matches);
print_r($matches);
Gives the following result:
Array
(
[0] => Array
(
[0] => 00/00/00->link
[1] => 00/00/00->link2
[2] => 00/00/00->link3
[3] => 00/00/00->link4
)
[1] => Array
(
[0] => 00/00/00
[1] => 00/00/00
[2] => 00/00/00
[3] => 00/00/00
)
[2] => Array
(
[0] => link
[1] => link2
[2] => link3
[3] => link4
)
)
your problem is that after using explode first time, it is turning into an array and explode function connat explode an array. You need to use a loop probablr for loop that targets array elemets then use explode function on those elements and you will have it.
See example Below:
<?php
$val="abc~~~def~~~ghi####jkl~~~mno~~~pqr###stu~~~vwx~~~yz1";
$val=explode("####", $val);
//result will be
$valWillBe=array(3) {
[0]=>'abc~~~def~~~ghi',
[1]=>'jkl~~~mno~~~pqr',
[2]=>'stu~~~vwx~~~yz1'
}
//if you want to explode again you use a loop
for($r=0; $r<sizeof($val); $r++){
$val[$r]=explode("~~~", $val[$r]);
}
//now you have your string exploded all in places.
?>

How to parse the list of urls and skip unneeded items (PHP)

I have a txt file with a lot of different urls. I want to parse the list and skip some urls to get final clean list. Please see part of the list below:
http://www.example.com/example1/
http://www.example.com/example2/
http://www.example.com/example3/
http://www.example.com/example4/
http://www.example.com/example.js
http://www.example.com/example.css
http://www.example.com/example1.js?v=123
http://www.example.com/{path}
http://www.example.com/feed/
http://www.example.com/?p=66
I want to skip all the urls like .js or .css or {path} or /feed/ or ?p=66 and output everything into txt file again. I want to do that using PHP. Any advice ?
<?php
$list = "http://www.example.com/example1/
http://www.example.com/example2/
http://www.example.com/example3/
http://www.example.com/example4/
http://www.example.com/example.js
http://www.example.com/example.css
http://www.example.com/example1.js?v=123
http://www.example.com/{path}
http://www.example.com/feed/
http://www.example.com/?p=66";
$arr = preg_split("/[\r\n]+/",$list);
// check our input array
print_r($arr);
$map = array();
foreach($arr as $v){
if(!preg_match("/({path}|\.(js|css)|\?p=\d+|\/feed\/)$/",$v)){
$map[] = $v;
}
};
// check our output array
print_r($map);
?>
This assumes you want to match urls that don't end with {path} or .css or .js or ?p=## (where # is a digit) or /feed/. This is why there is still the /example1.js?v=123 being matched. To make it match anywhere in the string, rather than only at the end, remove the $ from the end of the regular expression (just after the word feed).
My console output:
Array
(
[0] => http://www.example.com/example1/
[1] => http://www.example.com/example2/
[2] => http://www.example.com/example3/
[3] => http://www.example.com/example4/
[4] => http://www.example.com/example.js
[5] => http://www.example.com/example.css
[6] => http://www.example.com/example1.js?v=123
[7] => http://www.example.com/{path}
[8] => http://www.example.com/feed/
[9] => http://www.example.com/?p=66
)
Array
(
[0] => http://www.example.com/example1/
[1] => http://www.example.com/example2/
[2] => http://www.example.com/example3/
[3] => http://www.example.com/example4/
[4] => http://www.example.com/example1.js?v=123
)

How can I remove data from an array using regexp?

I have this array, $array :
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
)
and in this case, I am looking for any line that has ABS inside.
I could look for that by using the regexp http://.+ABS.+, and this will select the entire line.
But I still need to remove it from the array, not just replace it (or leave it empty.) But in this case, the array will become:
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_SM_138.jpg
)
Any ideas what method i need to use?
Thanks.
edit:
i am using OOP php
Use array_filter() with a custom callback.
Example:
function testABS($elem) {
return strpos($elem, 'ABS') === false;
}
print_r(array_filter($the_array, 'testABS'));
Note: This is a contrived example. You will need to adjust the logic in the callback function to meet your needs.
There's preg_grep:
$abs = preg_grep('/ABS/', $your_array);
and returns the matches as an array. It also has a flag to return only the non-matching entries, which is probably what you want: return all entries which do NOT have 'abs' in them.
What i understood is u want to remove that element from the array.
can do like this.
$arr = Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
);
$new_arr = array();
foreach($arr as $link){
if(!preg_match('ABS', $link)){
$new_arr[] = $link;
}
}
//ths will give array with only 4 elements as '18821_ABS_132.jpg' will be removed
return $new_arr;

PHP REGEX separate by different criteria

I'm trying to separate some strings by different criteria but I can't get the desired results.
Here are 3 examples:
$ppl[0] = "Balko, Vlado \"Panelбk\" (2008) {Byt na tretom (#1.55)}";
$ppl[1] = "'Abd Al-Hamid, Ja'far A Two Hour Delay (2001)";
$ppl[2] = "'t Hoen, Frans De reьnie (1963) (TV)";
I'm currently using this for the last 2:
$pattern = '#,|\t|\(#'
But I will get and empty space.
result:
Array ( [0] => 'Abd Al-Hamid [1] => Ja'far [2] => A Two Hour Delay [3] => 2001) )
Array ( [0] => 't Hoen [1] => Frans [2] => [3] => De reünie [4] => 1963) [5] => TV) )
As for the 1st expression I used another pattern but I still get empty spaces. Any ideas?
EDIT:
Thanks this helped indeed. I tried using a modified version on the first string:
$pattern4 = '#[",\t]+|[{}]+|[()]+#';
However I still get an empty space:
Array ( [0] => Balko [1] => Vlado [2] => Panelák [3] => [4] => 2008 [5] => [6] => Byt na tretom [7] => #1.55 [8] => [9] => )
What should I do? I think that the " and the brackets are causing the problem but I don't know how to fix it.
I would surmise you have two tabs as separator in your second and third example string. (Can't see that here, the SO editor converts them into spaces).
But you could adapt your regex slightly in that case:
$pattern = '#,|\t+|\(#'
Or simpler even:
$pattern = '#[,\t(]+#'
And the alternatve, btw, would be just applying array_filter() on the result arrays to remove the empty entries.

Problem with simple regular expression in PHP

The best way to explain my problem is to just show you.
Input String:
/04-11-2010/12:45/
Regular Expression to get date and time parts:
preg_match('#/(\d\d)-(\d\d)-(\d\d\d\d)/(\d\d):(\d\d)/#', $input, $matches);
PHP Matches Array:
Array
(
[0] => /01-11-2010/12:45/
[1] => 01
[2] => 11
[3] => 2010
[4] => 12
[5] => 45
)
Now the above regex works perfectly at getting the individual component parts that represent the date and time in the input string.
The problem is that the time part needs to be optional without bringing down the entire regular expression.
Problem Input String:
/04-11-2010//
PHP Matches Array
Array
(
)
Basically what I need to be returned by the matches array is:
Array
(
[0] => /01-11-2010/12:45/
[1] => 01
[2] => 11
[3] => 2010
[4] =>
[5] =>
)
Note array elements 4 and 5 still need to exist but return empty.
Use the question mark operator and a non-capturing group to make stuff optional.
#/(\d\d)-(\d\d)-(\d\d\d\d)/(?:(\d\d):(\d\d))?/#
I'm not sure how this interacts with the match array - if having the empty array elements is absolutely critical, you might need to instead go for
#/(\d\d)-(\d\d)-(\d\d\d\d)/((?:\d\d)?):?((?:\d\d)?)/#
Which has its own false-positives (the colon in the time is now optional).
Make the second part optional:
'#/(\d\d)-(\d\d)-(\d\d\d\d)/(?:(\d\d):(\d\d))?/#'
Here a non-capturing group (?:…) is used that cannot be referenced and thus doesn’t change the matching groups.
#/(\d\d)-(\d\d)-(\d\d\d\d)/((?:\d\d)?):?((?:\d\d)?)/#
does what you want (i.e. populates groups 4 and 5), but also accepts incomplete times like in
/04-11-2010/12:/
don't know if this is fine with you
I'm not a php-head, but how about:
preg_match('#/(\d\d)-(\d\d)-(\d\d\d\d)/(\d\d)?:?(\d\d)?/#', $input, $matches);
As far as regexps go, that should match a string that has no time field.
#OP, don't need messy regex.
$str="/04-11-2010/12:45/";
$s = array_filter(explode('/',$str));
$date=$s[1];
$time=$s[2];
$date_parts=explode("-",$date);
$time_parts=explode(":",$time);
if ( checkdate($date_parts[1],$date_parts[0],$date_parts[2]) ){
print "date ok\n";
}
Use native PHP functions for this task, using regular expressions is a bit of an overkill.
PHP 5 has the date_parse function:
$string = '/04-11-2010/12:45/';
$dateArray = date_parse(str_replace('/', ' ', $string));
print_r($dateArray);
$string = '/04-11-2010//';
$dateArray = date_parse(str_replace('/', ' ', $string));
print_r($dateArray);
Output:
Array
(
[year] => 2010
[month] => 11
[day] => 4
[hour] => 12
[minute] => 45
[second] => 0
[fraction] => 0
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
Array
(
[year] => 2010
[month] => 11
[day] => 4
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
PHP 5.3 has a more flexible date_parse_from_format function that you could also use.

Categories