I need to parse the values of (47.624718,-122.356133) or example or (lat,long) pair.
That url is generated by the following JS:
var html = \"<b>\" + name + \"</b> <br/>\" + address + \"<br><br>\" + \"<a id='myLink' href='result.php?\"+point+\"'>Professors here</a>\";
the point var is generated by this js var:
var point = new GLatLng(parseFloat(markers[i].getAttribute(\"lat\")),parseFloat(markers[i].getAttribute(\"lng\")));
Obviously these values vary in size/length based on point on google maps, so how do I parse this using php to get anything before the , and everything after the , besides the () into two variables?
Thanks
php > preg_match("/(-?[0-9]+(.[0-9]+)?),(-?[0-9]+(.[0-9]+)?)/","(42.3,-59.0)",$matches); print_r($matches);
Array
(
[0] => 42.3,-59.0
[1] => 42.3
[2] => .3
[3] => -59.0
[4] => .0
)
php >
The indexes are ordered like that because of the way the parenthesis are nested. Just use index 1 and 3.
you can write it more concisely then that, but it is easy to follow this way
$latlon = "(42.3,-59.0)";
list($lat, $lon) = string_to_lat_lon($latlon);
function string_to_lat_lon($string) {
$string = str_replace('(', '', $string); // remove leading bracket
$string = str_replace(')', '', $string); // remove trailing bracket
return explode(',', $string);
}
Related
I am scraping the following kind of strings from an external resource which I can't change:
["one item",0,0,2,0,1,"800.12"],
["another item",1,3,2,5,1,"1,713.59"],
(etc...)
I use the following code to explode the elements into an array.
<?php
$id = 0;
foreach($lines AS $line) {
$id = 0;
// remove brackets and line end comma's
$found_data[] = str_replace(array('],', '[',']', '"'), array('','','',''), $line);
// add data to array
$results[$id] = explode(',', $line);
}
Which works fine for the first line, but as the second line uses a comma for the thousands seperator of the last item, it fails there. So somehow I need to disable the explode to replace stuff between " characters.
If all values would be surrounded by " characters, I could just use something like
explode('","', $line);
However, unfortunately that's not the case here: some values are surrounded by ", some aren't (not always the same values are). So I'm a bit lost in how I should proceed. Anyone who can point me in the right direction?
You can use json_decode here since your input string appears to be a valid json string.
$str = '["another item",1,3,2,5,1,"1,713.59"]'
$arr = json_decode($str);
You can then access individual indices from resulting array or print the whole array using:
print_r($arr);
Output:
Array
(
[0] => another item
[1] => 1
[2] => 3
[3] => 2
[4] => 5
[5] => 1
[6] => 1,713.59
)
I am writing some code that expects to receive a block of html which is always going to include the following:
var code = [39.474, -0.3548];
The value between the square brackets is always going to be different. How could I extract the value between the square brackets using php?
I should probably point out that I am parsing a block of HTML to php that includes js aswell.
Use a regular expression like
preg_match_all('/\\[(.*?)\\]/', $your_string, $matches, PREG_SET_ORDER);
The array $matches[1] then will contain your values.
If the Javascript array format will be kept constant (always two components and a space after ','), you can use /(?:\[)(.*)[,][ ](.*)(?:\])/ regex to extract them in php:
$line = 'var code = [39.474, -0.3548];';
preg_match("/(?:\[)(.*)[,][ ](.*)(?:\])/", $line, $output_array);
print_r ($output_array);
The result will be
array(
0 => [39.474, -0.3548]
1 => 39.474
2 => -0.3548
)
A quick and dirty solution may be:
Replace "[" and "]"
explode by ","
$a = "[39.474, -0.3548]";
$b = str_replace("]", "", str_replace("[", "", $a));
$c = explode(",", $b);
print_r($c);
Output:
Array ( [0] => 39.474 [1] => -0.3548 )
I've been trying to get array of the fonts that I'm enqeueing on my wordpress theme. This is just for testing.
On input:
http://fonts.googleapis.com/css?family=Arimo:400,700|Quicksand:400,700|Cantarell:400,700,400italic,700italic|Muli:300,400,300italic,400italic|Roboto+Slab:400,700|Share:400,700,400italic,700italic|Inconsolata:400,700|Karla:400,700,400italic,700italic|Maven+Pro:400,500,700,900|Roboto+Slab:400,700|Open+Sans:400italic,600italic,700italic,400,600,700
What I need on output is like this:
array(
[0] => 'Arimo',
[1] => 'Quicksand',
[2] => 'Cantarell',
... so on
)
Till now, I have done almost everything but one little problem.
My code:
$input = 'http://fonts.googleapis.com/css?family=Arimo:400,700|Quicksand:400,700|Cantarell:400,700,400italic,700italic|Muli:300,400,300italic,400italic|Roboto+Slab:400,700|Share:400,700,400italic,700italic|Inconsolata:400,700|Karla:400,700,400italic,700italic|Maven+Pro:400,500,700,900|Roboto+Slab:400,700|Open+Sans:400italic,600italic,700italic,400,600,700';
$against = "/[A-Z][a-z]+[\+][A-Z][a-z]+|[A-Z][a-z]+/";
$matches = array()
preg_match_all( $against, $input, $matches );
print_r($matches);
From this, the output is like this:
array(
0 => Arimo
1 => Quicksand
2 => Cantarell
3 => Muli
4 => Roboto+Slab
5 => Share
6 => Inconsolata
7 => Karla
8 => Maven+Pro
9 => Roboto+Slab
10 => Open+Sans
)
There's the + sign where the font name has spaces. I want to get rid of that.
I'm not a regex expert. So, couldn't manage to do that.
Note: I know I could do it with str_replace() but don't want to go through that long process. I want to know if it's possible to escape the + sign through and leave an empty space there when we are collecting matched expressions.
Spaces encoded as plus (+) signs in url. You should decode your url.
$input = urldecode($input);
Without regex:
$query = strtr(substr(parse_url($url, PHP_URL_QUERY),7), '+', ' ');
$result = array_map(function ($i) { return explode(':', $i)[0]; }, explode('|', $query));
With regex:
if (preg_match_all('~(?:\G(?!\A)|[^?&]+[?&]family=)([^:|&]+):[^:|&]*(?:[|&#]|\z)~', strtr($url, '+', ' '), $m))
$result2 = $m[1];
From your code, output is given me something like this.
array([0] => array([0] => Arimo[1] => Quicksand[2] => Cantarell[3] => Muli[4] => Roboto+Slab[5] => Share[6] => Inconsolata[7] => Karla[8] => Maven+Pro[9] => Roboto+Slab[10] => Open+Sans))
if is correct, then i was solve this issue '+'. here is the solution.
$input = 'http://fonts.googleapis.com/css?family=Arimo:400,700|Quicksand:400,700|Cantarell:400,700,400italic,700italic|Muli:300,400,300italic,400italic|Roboto+Slab:400,700|Share:400,700,400italic,700italic|Inconsolata:400,700|Karla:400,700,400italic,700italic|Maven+Pro:400,500,700,900|Roboto+Slab:400,700|Open+Sans:400italic,600italic,700italic,400,600,700';
$against = "/[A-Z][a-z]+[\+][A-Z][a-z]+|[A-Z][a-z]+/";
$matches = array();
$newArr=array();
preg_match_all( $against, $input, $matches );
for($i=0;$i< count($matches);$i++){
for($j=0;$j< count($matches[$i]);$j++){
$string=preg_replace('/[^A-Za-z0-9\-]/', ' ', $matches[$j]);
if($string!=""){
$newArr[]=$string;
}
}
}
print_r($newArr);
In general, you have more than + characters to worry about.
Special characters, such as the ampersand (&), and non-ASCII characters in URL query parameters have to be escaped using percent-encoding (%xx). In addition, when an HTML form is submitted, spaces are encoded using the + character.
For example:
The font family "Jacques & Gilles" would be escaped as:
Jacques+%26+Gilles
The Unicode character U+1E99 (LATIN SMALL LETTER Y WITH RING ABOVE), serialized into octets as UTF-8 (E1 BA 99), would be escaped as:
%e1%ba%99
To do what you want properly, you have to extract the query string from the URL, and use parse_str() to extract the name=value pairs. The parse_str() function will automatically urldecode() the names and values including the + characters.
First, split the URL on the ? character to extract the query string:
$url = 'http://fonts.googleapis.com/css?family=Arimo:400,700|...|Maven+Pro:400,500,700,900|Roboto+Slab:400,700|...';
$a = explode ('?', $url, 2);
if (isset ($a[1])) {
$query = $a[1];
}
You can also use parse_url ($url, PHP_URL_QUERY), but it doesn't buy you much in this case.
Then extract all the parameters:
if (isset ($query)) {
parse_str ($query, $params);
if (isset ($params['family'])) {
/* OK: Extract family names. */
} else {
/* Error: No family parameter found. */
}
} else {
/* Error: No query string found. */
}
Note: You should always specify the second parameter of parse_str() to avoid clobbering existing variables.
I am using a explode and str_replace on the get parameter of the query string URL. My goal is to split the strings by certain characters to get to the value in the string that I want. I am having issues. It should work but doesn't.
Here are two samples of links with the query strings and delimiters I'm using to str_replace.
http://computerhelpwanted.com/jobs/?occupation=analyst&position=data-analyst
as you can see the URL above parameter is position and the value is data-analyst. The delimiter is the dash -.
http://computerhelpwanted.com/jobs/?occupation=analyst&position=business+systems+analyst
and this URL above uses same parameter position and value is business+systems+analyst. The delimiter is the + sign.
The value I am trying to get from the query string is the word analyst. It is the last word after the delimiters.
Here is my code which should do the trick, but doesn't for some reason.
$last_wordPosition = str_replace(array('-', '+')," ", end(explode(" ",$position)));
It works if the delimiter is a + sign, but fails if the delimiter is a - sign.
Anyone know why?
You have things in the wrong order:
$last_wordPosition = end(explode(" ", str_replace(array('-', '+'), " ", $position)));
You probably want to split it up so as to not get the E_STRICT error when not passing an variable to end:
$words = explode(" ", str_replace(array('-', '+'), " ", $position));
echo end($words);
Or something like:
echo preg_replace('/[^+-]+(\+|-)/', '', $position);
As #MarkB suggested you should use parse_url and parse_str since it is more appropriate in your case.
From the documentation of parse_url:
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
From the documentation of parse_str:
Parses str as if it were the query string passed via a URL and sets variables in the current scope.
So here is what you want to do:
$url1 = 'http://computerhelpwanted.com/jobs/?occupation=analyst&position=data-analyst';
$url2 = 'http://computerhelpwanted.com/jobs/?occupation=analyst&position=business+systems+analyst';
function mySplit($str)
{
if (preg_match('/\-/', $str))
$strSplited = split('-', $str);
else
$strSplited = split(' ', $str);
return $strSplited;
}
parse_str(parse_url($url1)['query'], $output);
print_r($values = mySplit($output['position']));
parse_str(parse_url($url2)['query'], $output);
print_r($values = mySplit($output['position']));
OUTPUT
Array
(
[0] => data
[1] => analyst
)
Array
(
[0] => business
[1] => systems
[2] => analyst
)
You said that you needed the last element of those values. Therefore you could find end and reset useful:
echo end($values);
reset($values);
Answering my own question to show how I ended up doing this. Seems like way more code than what the accepted answer is, but since I was suggested to use parse_url and parse_str but couldn't get it working right, I did it a different way.
function convertUrlQuery($query) {
$queryParts = explode('&', $query);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
return $params;
}
$arrayQuery = convertUrlQuery($_SERVER['QUERY_STRING']); // Returns - Array ( [occupation] => designer [position] => webmaster+or+website-designer )
$array_valueOccupation = $arrayQuery['occupation']; // Value of occupation parameter
$array_valuePosition = $arrayQuery['position']; // Value of position parameter
$split_valuePosition = explode(" ", str_replace(array('-', '+', ' or '), " ", $array_valuePosition)); // Splits the value of the position parameter into separate words using delimeters (- + or)
then to access different parts of the array
print_r($arrayQuery); // prints the array
echo $array_valueOccupation; // echos the occupation parameters value
echo $array_valuePosition; // echos the position parameters value
print_r($split_valuePosition); // prints the array of the spitted position parameter
foreach ($split_valuePosition as $value) { // foreach outputs all the values in the split_valuePosition array
echo $value.' ';
}
end($split_valuePosition); // gets the last value in the split_valuePosition array
implode(' ',$split_valuePosition); // puts the split_valuePosition back into a string with only spaces between each word
which outputs the following
arrayQuery = Array
(
[occupation] => analyst
[position] => data-analyst
)
array_valueOccupation = analyst
array_valuePosition = data-analyst
split_valuePosition = Array
(
[0] => data
[1] => analyst
)
foreach split_valuePosition =
- data
- analyst
end split_valuePosition = analyst
implode split_valuePosition = data analyst
How would I use regular expression with PHP to split the following string into 2 pars as depicted below:
String to be split: 4x330ml
Split into 4x330 and ml
I have tried the following but it does not return the accurate data:
$pdata = "4x330ml"
$data = preg_split('#(?<=\d)(?=[a-z])#i', $pdata);
Then I get something like 4 in $data[0] and x330 in $data[1]
EDIT: Please note that ml could also be cm, kg, etc. A little complicated, which I don't seem to find a solution.
EDIT: Also I have tried the following regex (which I prefer to use at this point) with incomplete results:
$pdata = "5x500ml";
$data = preg_split('/(\d+\.?\d+)|(\w+)i/', $pdata);
This returns:
Array
(
[0] => 5x
[1] => ml
)
500 from that string is not being returned...
Thanks,
You said it could be ml, cm, or kg. and that you don't have to use regex. So, assuming it is always 2 characters at the end, a very simple way to do this would be:
$string = "4x330ml";
$part1 = substr($string, 0, -2); //returns 4x330
$part2 = substr($string, -2, 2); //returns "ml"
This ought to give you what you want.
$pdata = "4x330cm";
$data = preg_match('/([^(ml|cm|kg)]*)(ml|cm|kg)/', $pdata, $group);
echo $group[0].' ';
echo $group[1].' ';
echo $group[2].' ';
Use the preg_match function, and store the results into an array. The 0 index will return the entire matched string. The $group[1] will return just "4x330". The $group[2]$ will return just the "cm".
I'd use preg_match:
$pdata = "4x330ml";
preg_match('/(\d+x\d+)(.*)/',$pdata, $m);
print_r($m);
Output:
Array
(
[0] => 4x330ml
[1] => 4x330
[2] => ml
)
Assuming the units will always be 2 characters long you can use this
$pdata = "4x330ml";
$strNumber = substr($pdata, 0, strlen($pdata)-2);
$strUnit = substr($pdata, strlen($pdata)-2,2);
echo $strNumber; //returns 4x330
echo $strUnit; //returns ml
you can do this without a regex
$string = '4x330ml';
$splitPoint = strrpos($string,'m');
$firstPart = substr($string,0,$string); //gets 4x330
$secondPart = substr($string,$splitPoint); //gets ml
I was able to solve this problem by using the following code:
$data = "4x500ml";
$pdata = preg_split('/(\d+x\d+)/', $data, NULL, PREG_SPLIT_DELIM_CAPTURE);
Which now prints:
array(
[0] =>
[1] => 4x500
[2] => ml
)
It looks like it is capturing the delimeter as array[1] and since the delimeter is actually the first section of the string I want to split, it is fine for now, until I find a better solution.
Thank you all for trying.