php address into latitude and longitude - php

i am trying to convert the address into latitude.here's my code.
but its giving wrong latititude. if i put the address manually at $final place it gives the correct latitude.however i cant hard code it,whatever source comes in text box i have to find out that address latitude. what is the mistake?
<?php
$source=$_POST['textfield11'];
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$only = str_replace(',', '+', $source);
$final = str_replace(' ', '+', $only);
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$final&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>

Instead of using str_replace, you should use urlencode.
<?php
$source=urlencode($_POST['textfield11']);
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$source&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>

<?
$final = urlencode($source);
$geo = simplexml_load_file("http://maps.google.com/maps/api/geocode/xml?address={$final}&sensor=false");
$lat = $geo->result->geometry->location->lat;
$lng = $geo->result->geometry->location->lng;
?>

Add a space after your , in the str_replace ... Cos at the moment, if the user types an address as "Somewhere road, That town" it will replace the ',' with a plus, and then the space resulting in: "Somewhere+road++That town" ... So something like this:
<?php
$source=$_POST['textfield11'];
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$only = str_replace(', ', '+', $source);
$final = str_replace(' ', '+', $only);
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$final&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>

Related

PHP regular expression - number (price) only

I am trying to remove .00 from price field. I have price output like this:
127,000.00 €
My regex expression is:
<?php $site = get_field('price'); $site = preg_replace( '/[^0-9]/', '', $site ); $site = rtrim($site, ''); echo $site;?>
Result is:
12700000
It should be:
127000
Can anybody help me with this?
A better aproch with PHP 7+...
Ignoring the decimals:
$site = '127,000.00 €';
$site = (int) preg_replace('/\..*|\D/', NULL, $site);
echo $site;
// 127000
Rounding:
$site = '127,000.50 €';
$site = round(preg_replace('/[^\d.]/', NULL, $site));
echo $site;
// 127001
$amount = "127,000.00 €";
$amount = explode(" ",$amount);
$amount = $amount[0];
$amount = str_replace( ',', '', $amount );
echo (int) $amount ;
output: 127000
$amtstr = explode(' ', "127,000.00 €");
$value = round(str_replace(',','',$amtstr[0])).' '.$amtstr[1];
echo $value;
Hope this may help you

How to get Implode get data before this string?

My url is
likehttp://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/
how could i get Crossing Republic
using php
I used
<?php
list($a,$page_get) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$page_get);
$getCity1=remove_dash($array[0]);
$p=$array[1];
$get_city = implode('-',$getCity1);
print_r($get_city);
?>
but its giving
Crossing republik tutor
while I don't want tutor
use explode function and take the last
$req_uris = explode('/',$_SERVER['REQUEST_URI']);
echo $req_uris[count($req_uris)-1];
and if you want you can replace dash with space
echo str_replace('-', ' ', $req_uris[count($req_uris)-1]);
EDIT
$url = 'http://localhost/manishatutors/tutors-in-city/Crossing-Republik-tutor/';
$exploded = array_values(array_filter(explode('/',$url)));
$last = $req_uris[count($exploded)-1];
echo str_replace( '-', ' ', str_replace('tutor', '', $last) );
change $url with $_SERVER['REQUEST_URI']
You may try this
<?php
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=remove_dash($array[0]);
$p=$array[1];
$val3= implode('-',$val2);
print_r($val3);
?>
NEW EDITED ANSWER
list($a,$val) = explode("city/",$_SERVER['REQUEST_URI']);
$array=explode("/",$val);
$val2=$array[0];
$p=$array[1];
$val3= implode('-tutor',$val2);
print_r(remove_dash($val3[0]));
?>

Extract specific word from a string

It might seem easy to do but I have trouble extracting this string. I have a string that has # tags in it and I'm trying to pull the tags maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa
And here is what I want to extract 33.536759,-7.613825,17z :
$var = preg_match_all("/#(\w*)/",$path,$query);
Any way I can do this? Much appreciated.
Change your regex to this one: /#([\w\d\.\,-]*)/.
This will return the string beginning with #.
$string = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
$string = explode('/',$string);
//$coordinates = substr($string[3], 1);
//print_r($coordinates);
foreach ($string as $substring) {
if (substr( $substring, 0, 1 ) === "#") {
$coordinates = $substring;
}
}
echo $coordinates;
This is working for me:
$path = "maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa";
$var = preg_match_all("/#([^\/]+)/",$path,$query);
print $query[1][0];
A regex would do.
/#(-*\d+\.\d+),(-*\d\.\d+,\d+z*)/
If there is only one # and the string ends with / you can use the following code:
//String
$string = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
//Save string after the first #
$coordinates = strstr($string, '#');
//Remove #
$coordinates = str_replace('#', '', $coordinates);
//Separate string on every /
$coordinates = explode('/', $coordinates );
//Save first part
$coordinates = $coordinates[0];
//Do what you want
echo $coordinates;
do like this
$re = '/#((.*?),-(.*?),)/mi';
$str = 'maps/place/Residences+Jardins+de+Majorelle/#33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa';
preg_match_all($re, $str, $matches);
echo $matches[2][0].'<br>';
echo $matches[3][0];
output
33.536759
7.613825

php match a string from other string

I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $result='aa,bb,dd,ee'.
I am not able to get te result as desired as not sure which PHP function can give the desired output.
Also if I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $match=''. i.e if $match is found in $test then $match value can be skipped
Any help will be really appreciated.
You can try with:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$output = trim(str_replace(',,', ',', str_replace($match, '', $test), ','));
or:
$testArr = explode(',', $test);
if(($key = array_search($match, $testArr)) !== false) {
unset($testArr[$key]);
}
$output = implode(',', $testArr);
Try with preg_replace
$test = 'aa,bb,cc,dd,ee';
$match ='cc';
echo $new = preg_replace('/'.$match.',|,'.$match.'$/', '', $test);
Output
aa,bb,dd,ee
$test = 'aa,bb,cc,dd,ee';
$match='cc';
echo trim(str_replace(',,', ',' , str_replace($match,'',$test)),',');
DEMO
Try this:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$temp = explode(',', $test);
unset($temp[ array_search($match, $temp) ] );
$result = implode(',', $temp);
echo $result;

Strip array of url and other characters, show only post name

The xml is like this: (wordpress url's) I want to strip them and get only the posts words.
http://www.site1.com/dir/this-is-page/
http://www.site2.com/this-is-page
How do i strip the url's and get only "this is page" (without the rest of the urls, and the "-") if i have two diffrent types of urls; one with dir and one without dir? Sample code bellow:
$feeds = array('http://www.site1.com/dir/feed.xml', 'http://www.site2.com/feed.xml');
foreach($feeds as $feed)
{
$xml = simplexml_load_file($feed);
foreach( $xml->url as $url )
{
$loc = $url->loc;
echo $loc;
$locstrip = explode("/",$loc);
$locstripped = $locstrip[4];
echo '<br />';
echo $locstripped;
echo '<br />';
mysql_query("TRUNCATE TABLE interlinks");
mysql_query("INSERT INTO interlinks (title, url) VALUES ('$locstripped', '$loc')");
}
}
?>
TY
Ty guys, did it like this:
$urlstrip = basename($loc);
$linestrip = str_replace(array('-','_'), ' ', $urlstrip);
You want only the last segment of the URL?
Try something like this.
$url = trim('http://www.site1.com/dir/this-is-page/', '/');
$url = explode('/', $url);
$url = array_pop($url);
$url = str_replace(array('-','_'), ' ', $url);
It's not very elegant... but it works.
replace
$locstripped = $locstrip[4];
with
$locstripped = $locstrip[count($loc) - 1];
if(!$locstripped)
$locstripped = $locstrip[count($loc) - 2];
$locstripped = str_replace('-', ' ', $locstripped);

Categories