Jump to content

Recommended Posts

Hello,

Id like to know how to extract bold coordinates from this url (PHP). I have never done that kind of stuff before.

 

Important: Not just  this specific url, but all urls with different coordinates every time.

 

http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12

 

 

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/259691-get-coordinates-from-url/
Share on other sites

Hi Klepec!

 

Input:

http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12

 

Code:

<?php
$string='http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12';
$regex=',http://maps\.google\.com/maps\?q=\K[^&]+,';
preg_match($regex,$string,$m);
echo $m[0].'<br />';
?>

 

Output:

46.055603,14.507732

 

Let me know if this works for you and if you have any questions.

:)

Hello,

 

First of all, thanks to both of you for taking your time to help me solve this out.

 

@ragax:

 

It works JUST with the given url.

If i try to use something else, there is an error:

Notice: Undefined offset: 0 in E:\stuff\www\test\test.php on line 22

 

Line 22:

echo $m[0].'<br />';

 

@.josh:

Works like a charm with any google maps link.

Can you guys tell me what are the differences (advantages/disad.) between regex and non regex version. Are there any? Thanks :)

 

 

 

 

 

It works JUST with the given url.

 

Mmm... You mean, it only works with URLs that contain coordinates (not just with the specific coordinates in the sample string).

Yes, the code assumes you were feeding it a url with coordinates, per your request.

If not, just add an IF:

 

Code:

<?php
$string=array('http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12','http://google.com','http://maps.google.com/maps?q=99.999,00.000&num=1&t=m&z=12');
$regex=',http://maps\.google\.com/maps\?q=\K[^&]+,';
foreach ($string as $url)
if (preg_match($regex,$url,$m)) 
echo $m[0].'<br />';
else echo 'no coordinates'.'<br />';
?>

 

Output:

46.055603,14.507732

no coordinates

99.999,00.000

 

Let me know if you have any questions.

:)

typically string functions run much faster

 

Ah, AyKay, I often agree with you, but for this specific case. I checked because it surprised me that parse_url would be faster than a straight regex. (Maybe someone knows: is parse_url full of regex, or built from scratch?).

Here's the result of benchmarking, using .josh's very own (and very cool) benchmarking function:

 

times executed (each): 500000

fastest to slowest:

 

Array

(

    [ragax] => 0.00001112222897780482

    [josh] => 0.00001650101801627486

)

 

biggest difference time: 0.00000537878903847004

fastest is 48.3607% faster than the slowest

 

 

The code is below if anyone is interested.

Wishing you all a beautiful day. :)

 

function josh()
{
$string='http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12';
$parts = parse_url($string);
parse_str($parts['query'],$params);
return $params['q'];
}
echo josh().'<br />';

function ragax()
{
$string='http://maps.google.com/maps?q=46.055603,14.507732&num=1&t=m&z=12';
$regex=',http://maps\.google\.com/maps\?q=\K[^&]+,';
preg_match($regex,$string,$m);
return $m[0];
}
echo ragax().'<br />';

benchmark(500000,'ragax','josh');

function benchmark ($rounds=100) {
// from .josh on http://www.phpfreaks.com/forums/index.php?topic=338139.0
// syntax: benchmark(10000,'lazy_matchall','greedy_matchall','negative_lookahead');
  // if an integer is not passed as first argument, default to 100
  if (!is_integer($rounds)) $rounds = 100;
  // get the rest of the arguments passed to the function
  $funcs = func_get_args();

  // remove first argument from the list, since it is $rounds
  array_shift($funcs);
  // for each user defined function...
  foreach ($funcs as $func) {
    // if the function doesn't exist, skip it
    if (!function_exists($func)) break; 
    $time = array();
    // call the function the specified/default amount of times
    for ($c = 0; $c < $rounds; $c++) {
      // get the current microtime timestamp
      $start = explode(" ",microtime());
      $start = $start[0];

      // call the user defined function
      $func();

      // get the current microtime timestamp
      $end = explode(" ",microtime());
      $end = $end[0];
      // find out the difference between the two
      $diff = bcsub($end,$start,20);

      // for some unknown reason, $diff occasionally returns a negative number
      // dunno if it's a bug in bcsub or a bug in microtime or maybe sometimes
      // it just goes so damn fast it goes backwards in time! anyways...let's
      // just weed it out, make sure it doesn't skew the results. 
      if ($diff > 0) $time[$c] = $diff;
    } // end $c
    // get the average time
    $average[$func] = rtrim(bcdiv(array_sum($time),count($time),20),'0');
  } // end foreach funcs

  // sort the averages ascending (preserving the keys, because we used the  
  // user defined function name as the array keys
  asort($average,SORT_NUMERIC);
  // get the fastest average
  $fastest = max($average);
  // get the slowest average
  $slowest = min($average);

  // display how many times we executed each function
  echo "times executed (each): ".$rounds . "<br/>";
  // display the averages
  echo "fastest to slowest:<br/><pre>";print_r($average); echo "</pre>";
  // display the time difference between slowest and fastest
  echo "biggest difference time: " . rtrim(bcsub($fastest,$slowest,20),'0') . "<br/>";
  // calculate and display how much faster the fastest one was, compared
  // to the slowest, as a percentage
  $percent = rtrim(round(bcmul(bcsub(bcdiv($fastest,$slowest,20),1,20),100,20),4),'0');

  echo "fastest is " . $percent . "% faster than the slowest";
} 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.