从字符串中提取特定单词

时间:2014-06-04 14:19:54

标签: php string preg-match

这似乎很容易,但我无法提取此字符串。我有一个包含@个标记的字符串,我试图提取标记maps/place/Residences+Jardins+de+Majorelle/@33.536759,-7.613825,17z/data=!3m1!4b1!4m2!3m1!1s0xda62d6053931323:0x2f978f4d1aabb1aa

以下是我要提取的33.536759,-7.613825,17z

$var = preg_match_all("/@(\w*)/",$path,$query);

我能做到这一点吗?非常感谢。

6 个答案:

答案 0 :(得分:2)

这将返回以@开头的字符串。

$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;

答案 1 :(得分:2)

将正则表达式更改为此/@([\w\d\.\,-]*)/

答案 2 :(得分:2)

正则表达式会这样做。

   /@(-*\d+\.\d+),(-*\d\.\d+,\d+z*)/

答案 3 :(得分:2)

如果只有一个@并且字符串以/结尾,则可以使用以下代码:

//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;

答案 4 :(得分:2)

这对我有用:

$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];

答案 5 :(得分:1)

喜欢这个

$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];

<强> 输出

33.536759
7.613825