Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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_Regex - Fatal编程技术网

Php 返回字符最后一次出现之间的字符串

Php 返回字符最后一次出现之间的字符串,php,regex,Php,Regex,我试图提取一个介于逗号“,”和右括号之间的字符串” 这是下面的字符串 (美国佛罗里达州蓬塔戈尔达) 我希望从中提取U.S.A 我当前使用的正则表达式返回美国FL 这是通过以下正则表达式(?使用此正则表达式来完成的: (?<=,\s)[^,]*(?=\)) (?改用这个: (?<=,\s)[^,]*(?=\)) (?我要用这个正则表达式: ([^,\s]+)(?=\)) UPD: $str = '(london, United kingdom)'; preg_match('~(

我试图提取一个介于逗号“,”和右括号之间的字符串”

这是下面的字符串

(美国佛罗里达州蓬塔戈尔达)

我希望从中提取
U.S.A

我当前使用的正则表达式返回美国FL

这是通过以下正则表达式
(?使用此正则表达式来完成的:

(?<=,\s)[^,]*(?=\))
(?改用这个:

(?<=,\s)[^,]*(?=\))

(?我要用这个正则表达式:

([^,\s]+)(?=\))
UPD

$str = '(london, United kingdom)';

preg_match('~([^,]+)(?=\))~', $str, $matches);

var_dump($matches);

我会用这个正则表达式:

([^,\s]+)(?=\))
UPD

$str = '(london, United kingdom)';

preg_match('~([^,]+)(?=\))~', $str, $matches);

var_dump($matches);

如果您不想使用正则表达式,这也可以:

$string = "(Punta Gorda, FL, U.S.A.)";
$last_occurence = strrpos($string, ",");
$country = substr($string, $last_occurence + 1, strlen($string) - $last_occurence - 2);

如果您不想使用正则表达式,这也可以:

$string = "(Punta Gorda, FL, U.S.A.)";
$last_occurence = strrpos($string, ",");
$country = substr($string, $last_occurence + 1, strlen($string) - $last_occurence - 2);

此解决方案存在轻微问题,它不会像英国那样注册地点,例如
(英国伦敦)
@mk_89:已更新,但如果需要,现在需要调整值。此解决方案存在轻微问题,它不会像英国那样注册地点,例如
(英国伦敦)
@mk_89:已更新,但如果需要,您现在需要修剪该值。好的,谢谢,我已使用其他输入(如
英国
)对其进行了测试,效果良好。好的,谢谢,我已使用其他输入(如
英国
)对其进行了测试,效果良好。