0

我使用商店坐标并手动将坐标作为 lang 和 long 添加到数据库中。有时会错误地批准坐标。

让我通过一个例子来解释。

例如; 朗是33.4534543543。但有时我会错误地按下键盘,它变得像,

33.4534543543<

或 33.4534543543,或 ,(空格)33.4534543543<

我怎样才能只得到 33.4534543543?

4

2 回答 2

0

听起来你想做一个preg_match: http: //phpfiddle.org/main/code/z6q-a1d

$old_vals = array(
    '33.4534543543<',
    '33.4534543543,',
    ', 33.4534543543<'
);

$new_vals = array();

foreach ($old_vals as $val) {
    preg_match('(\d*\.?\d+)',$val, $match);
    array_push($new_vals, $match[0]);
}

print_r($new_vals);

输出

Array (
    [0] => 33.4534543543,
    [1] => 33.4534543543,
    [2] => 33.4534543543
)
于 2013-08-20T20:12:44.143 回答
0

preg_match_all

要从包含多个匹配项的字符串中查找匹配项,您可以使用preg_match_all

$strings = "33.4534543543<
33.4534543543,
, 33.4534543543<";
$pattern = "!(\d+\.\d+)!";

preg_match_all($pattern,$strings,$matches);

print_r($matches[0]);

输出

Array
(
    [0] => 33.4534543543
    [1] => 33.4534543543
    [2] => 33.4534543543
)

预匹配

要从单个字符串中查找匹配项,您可以使用preg_match.

$string = "33.4534543543<";
$pattern = "!(\d+\.\d+)!";

if(preg_match($pattern,$string,$match)){
print($match[0]);
}

输出

33.4534543543

preg_replace

要替换现有字符串中不想要的任何内容,您可以使用preg_replace

$string = preg_replace('![^\d.]!','',$string);

一个例子:

$strings = "33.4534543543<
33.4534543543,
, 33.4534543543<";

$strings_exp = explode("\n",$strings);

$output = '';
foreach($strings_exp as $string){
$output.= "String '$string' becomes ";
$new_string = preg_replace('![^0-9.]!','',$string);
$output.= "'$new_string'\n";
}

echo $output;

输出

String '33.4534543543<' becomes '33.4534543543'
String '33.4534543543,' becomes '33.4534543543'
String ', 33.4534543543<' becomes '33.4534543543'
于 2013-08-20T20:13:13.667 回答