Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配字符串的PHP_Php - Fatal编程技术网

匹配字符串的PHP

匹配字符串的PHP,php,Php,我有一个印度公司数据集,需要从地址字段中提取城市和邮政编码: 地址字段示例: 印度卡鲁尔泰米尔纳德邦卡鲁尔L.G.B.附近Sengunthapuram邮政站Gowripuram West-639 002 正如你所看到的,城市是卡鲁尔,拉链紧跟在-(连字符)后面 我需要PHP代码来匹配[city]-[zip] 不知道如何做到这一点我可以找到后,炒作拉链,但不知道如何找到城市,请注意城市可以是2个字 为你的时间干杯/ J您可以使用explode创建所有字段的数组,您可以在连字符上拆分它们。那么,在一

我有一个印度公司数据集,需要从地址字段中提取城市和邮政编码:

地址字段示例: 印度卡鲁尔泰米尔纳德邦卡鲁尔L.G.B.附近Sengunthapuram邮政站Gowripuram West-639 002

正如你所看到的,城市是卡鲁尔,拉链紧跟在-(连字符)后面

我需要PHP代码来匹配[city]-[zip]

不知道如何做到这一点我可以找到后,炒作拉链,但不知道如何找到城市,请注意城市可以是2个字

为你的时间干杯/


J

您可以使用explode创建所有字段的数组,您可以在连字符上拆分它们。那么,在一个数组中有2个值。第一个是你的城市(可以是两个字),第二个是你的拉链

$info= explode("-",$adresfieldexample);

我推荐正则表达式。如果您反复使用它,性能应该很好,因为您可以预编译表达式。

以下正则表达式将“Karur”放在
$matches[1]
中,将“639 002”放在
$matches[2]

它也适用于多词城市名称

$str = "Gowripuram West, Sengunthapuram Post, Near L.G.B., Karur, Tamilnadu, Karur - 639 002, India";

preg_match( '/.+, (.+) - ([0-9]+ [0-9]+),/', $str, $matches);

print_r($matches);
Regex可能可以改进,但我相信它符合您问题中指定的要求。

尝试以下方法:

<?php
$address = "Gowripuram West, Sengunthapuram Post, Near L.G.B., Karur, Tamilnadu, Karur - 639 002, India";

// removes spaces between digits.
$address = preg_replace('{(\d)\s+(\d)}','\1\2',$address);

// removes spaces surrounding comma.
$address = preg_replace('{\s*,\s*}',',',$address);
var_dump($address);

// zip is 6 digit number and city is the word(s) appearing betwwen zip and previous comma.
if(preg_match('@.*,(.*?)(\d{6})@',$address,$matches)) {
    $city = trim($matches[1]);
    $zip = trim($matches[2]);
}

$city = preg_replace('{\W+$}','',$city);

var_dump($city);    // prints Karur
var_dump($zip);     // prints 639002

?>
<?php

$str  = "Gowripuram West, Sengunthapuram Post, Near L.G.B., Karur, Tamilnadu, Karur - 639 002, India";
$res  = substr($str,strpos($str, "," ,3), strpos($str,"\r"));
//this results in " Karur - 639 002, India";

$ruf  = explode($res,"-");
//this results in 
//$ruf[0]="Karur " $ruf[1]="639 002, India";

$city    = $ruf[0];
$zip     = substr($ruf[1],0,strpos($ruf[1], ",");
$country = substr($ruf[1],strpos($ruf[1],","),strpos($ruf[1],"\r"));

?>

正则表达式在所有应用程序中都占有一席之地,但在不同的国家/语言中,您可能会增加不必要的复杂性,从而占用少量的处理时间

试试这个:

<?php
$address = "Gowripuram West, Sengunthapuram Post, Near L.G.B., Karur, Tamilnadu, Karur - 639 002, India";

// removes spaces between digits.
$address = preg_replace('{(\d)\s+(\d)}','\1\2',$address);

// removes spaces surrounding comma.
$address = preg_replace('{\s*,\s*}',',',$address);
var_dump($address);

// zip is 6 digit number and city is the word(s) appearing betwwen zip and previous comma.
if(preg_match('@.*,(.*?)(\d{6})@',$address,$matches)) {
    $city = trim($matches[1]);
    $zip = trim($matches[2]);
}

$city = preg_replace('{\W+$}','',$city);

var_dump($city);    // prints Karur
var_dump($zip);     // prints 639002

?>
<?php

$str  = "Gowripuram West, Sengunthapuram Post, Near L.G.B., Karur, Tamilnadu, Karur - 639 002, India";
$res  = substr($str,strpos($str, "," ,3), strpos($str,"\r"));
//this results in " Karur - 639 002, India";

$ruf  = explode($res,"-");
//this results in 
//$ruf[0]="Karur " $ruf[1]="639 002, India";

$city    = $ruf[0];
$zip     = substr($ruf[1],0,strpos($ruf[1], ",");
$country = substr($ruf[1],strpos($ruf[1],","),strpos($ruf[1],"\r"));

?>

这可能是个愚蠢的问题,但城市名称是否可以包含逗号或数字?