PHP/Regex:从字符串中提取字符串
我刚开始使用PHP,希望这里有人能帮我 我试图从另一个字符串(“PHP/Regex:从字符串中提取字符串,php,regex,str-replace,substr,strstr,Php,Regex,Str Replace,Substr,Strstr,我刚开始使用PHP,希望这里有人能帮我 我试图从另一个字符串(“mainString”)中提取一个字符串(“myRegion”),其中我的字符串总是以“myCountry:”开头,以分号(;)结尾)如果主字符串在myCountry之后包含更多国家/地区,或者如果主字符串在myCountry之后没有包含更多国家/地区,则不包含任何内容 显示主字符串不同选项的示例: $myRegions = strstr($mainString, $myCountry); $m
mainString
”)中提取一个字符串(“myRegion
”),其中我的字符串总是以“myCountry:
”开头,以分号(;
)结尾)如果主字符串在myCountry之后包含更多国家/地区,或者如果主字符串在myCountry之后没有包含更多国家/地区,则不包含任何内容
显示主字符串不同选项的示例:
$myRegions = strstr($mainString, $myCountry);
$myRegions = str_replace($myCountry . ": ", "", $myRegions);
$myRegions = substr($myRegions, 0, strpos($myRegions, ";"));
- 我的国家:地区1、地区2
- 我的国家:地区1、地区2、地区3;其他国家:地区1
- 其他国家:区域1;我的国家:地区1;其他国家:地区1、地区2
$myRegions = strstr($mainString, $myCountry);
$myRegions = str_replace($myCountry . ": ", "", $myRegions);
$myRegions = substr($myRegions, 0, strpos($myRegions, ";"));
非常感谢你在这方面的帮助,迈克 使用正则表达式:
preg_match('/myCountry\:\s*([^\;]+)/', $mainString, $out);
$myRegion = $out[1];
使用正则表达式:
preg_match('/myCountry\:\s*([^\;]+)/', $mainString, $out);
$myRegion = $out[1];
从评论中可以看出,您似乎对非正则表达式解决方案感兴趣,而且您是一名初学者,对学习感兴趣,因此这里有另一种可能的方法使用。 (希望这不是不必要的) 首先,要认识到你的定义是由
分开的代码>因为它是:
我国:地区1、地区2、地区3
代码>
其他国家/地区:地区1
因此,使用,您可以生成定义数组:
$string = 'otherCountry: region1; myCountry: region1; otherCountry: region2, region3';
$definitions = explode (';', $string);
给你
array(3) {
[0]=>
string(21) "otherCountry: region1"
[1]=>
string(19) " myCountry: region1"
[2]=>
string(31) " otherCountry: region2, region3"
}
现在,您可以(使用)迭代该数组并使用:
分解它,然后使用,
分解该数组的第二个结果。
通过这种方式,您可以建立一个关联数组,将您的国家与其各自的区域关联起来
$result = array();
foreach ($definitions as $countryDefinition) {
$parts = explode (':', $countryDefinition); // parting at the :
$country = trim($parts[0]); // look up trim to understand this
$regions = explode(',', $parts[1]); // exploding by the , to get the regions array
if(!array_key_exists($country, $result)) { // check if the country is already defined in $result
$result[$country] = array();
}
$result[$country] = array_merge($result[$country], $regions);
}
这是一个非常好的玩法。从评论中可以看出,您似乎对非正则表达式解决方案感兴趣,而且您是初学者,对学习感兴趣,因此这里有另一种可能的方法使用。
(希望这不是不必要的)
首先,要认识到你的定义是由分开的代码>因为它是:
我国:地区1、地区2、地区3
代码>
其他国家/地区:地区1
因此,使用,您可以生成定义数组:
$string = 'otherCountry: region1; myCountry: region1; otherCountry: region2, region3';
$definitions = explode (';', $string);
给你
array(3) {
[0]=>
string(21) "otherCountry: region1"
[1]=>
string(19) " myCountry: region1"
[2]=>
string(31) " otherCountry: region2, region3"
}
现在,您可以(使用)迭代该数组并使用:
分解它,然后使用,
分解该数组的第二个结果。
通过这种方式,您可以建立一个关联数组,将您的国家与其各自的区域关联起来
$result = array();
foreach ($definitions as $countryDefinition) {
$parts = explode (':', $countryDefinition); // parting at the :
$country = trim($parts[0]); // look up trim to understand this
$regions = explode(',', $parts[1]); // exploding by the , to get the regions array
if(!array_key_exists($country, $result)) { // check if the country is already defined in $result
$result[$country] = array();
}
$result[$country] = array_merge($result[$country], $regions);
}
这只是一个很好的例子。FYI:您的示例中没有使用正则表达式str_replace
不使用REGEX-preg_replace
我知道吗,只是想在这里提一下,以防它是一个更好的选择。仅供参考:您的示例中没有使用正则表达式str_replace
不使用REGEX-preg_replace
我知道吗,只是想在这里提一下,以防它是一个更好的选择。谢谢!一个问题:如果myCountry之后会有更多的国家,这会一直持续到myCountry之后的第一个分号吗(这就是我需要的)?是的。它只需要从myCountry:
到第一个的所有内容代码>很棒-非常感谢。确认它工作得很好,甚至比预期的还要快。谢谢!一个问题:如果myCountry之后会有更多的国家,这会一直持续到myCountry之后的第一个分号吗(这就是我需要的)?是的。它只需要从myCountry:
到第一个的所有内容代码>很棒-非常感谢。确认它工作得很好,甚至比预期的还要快。我在简单示例的链接上得到了502个错误,它死了?@Sunchock eval.in似乎已关闭/关闭。我在另一个平台上为同一代码添加了一个新链接。供将来参考:您也可以复制第一个代码块和最后一个代码块,然后自己尝试。谢谢,我为您的数组示例推荐了一个eddit,因为它有点混乱。数组有3个条目,但您只写了2个条目。它们与您的第一句匹配,但与explode的代码部分不匹配。@Sunchock我没有看到您的编辑,但我想我已经修复了它?我没有看到更改数组(3){[0]=>string(19)“myCountry:region1,region2,region3”[1]=>string(31)“otherCountry:region1}将变成数组(3){[0]=>string(19)“otherCountry:region1”[1]=>字符串(31)“myCountry:region1”[2]=>字符串(31)“otherCountry:region2,region3”}因为您的数组与第一句匹配,但与使用Explode的示例不匹配。我在简单示例的链接上得到502个错误,它死了?@Sunchock eval.in似乎已关闭/关闭。我在另一个平台上为同一代码添加了一个新链接。供将来参考:您也可以复制第一个代码块和最后一个代码块,然后自己尝试。谢谢,我为您的数组示例推荐了一个eddit,因为它有点混乱。数组有3个条目,但您只写了2个条目。它们与您的第一句匹配,但与explode的代码部分不匹配。@Sunchock我没有看到您的编辑,但我想我已经修复了它?我没有看到更改数组(3){[0]=>string(19)“myCountry:region1,region2,region3”[1]=>string(31)“otherCountry:region1}将变成数组(3){[0]=>string(19)“otherCountry:region1”[1]=>字符串(31)“myCountry:region1”[2]=>字符串(31)“otherCountry:region2,region3”}因为数组与第一句匹配,但与使用explode的示例不匹配