PHP返回匹配许多不同的字符串
我有许多不同的字符串,例如: php 我需要的是一种从字符串开始返回多次出现的匹配项的方法 php 我曾想过将每个字符串中的第一个单词拆分,然后返回计数为PHP返回匹配许多不同的字符串,php,regex,string,Php,Regex,String,我有许多不同的字符串,例如: php 我需要的是一种从字符串开始返回多次出现的匹配项的方法 php 我曾想过将每个字符串中的第一个单词拆分,然后返回计数为>1的任意一个,但我的问题是,它可能会超过第一个单词 这是一个虚构的示例,实际数据将有大约400个字符串,因此如果它在计算上很耗时,那么它应该不会成为问题,在应用程序生命周期中只会发生一次或两次 有人能帮上忙吗?这里有一个简单的方法来计算数组中每个元素的字数。它考虑将名称与位置分开,如果找到一个或多个名称,则最后一个名称的末尾将只有一个字符或“
>1
的任意一个,但我的问题是,它可能会超过第一个单词
这是一个虚构的示例,实际数据将有大约400个字符串,因此如果它在计算上很耗时,那么它应该不会成为问题,在应用程序生命周期中只会发生一次或两次
有人能帮上忙吗?这里有一个简单的方法来计算数组中每个元素的字数。它考虑将名称与位置分开,如果找到一个或多个名称,则最后一个名称的末尾将只有一个字符或“.”:
<?php
$names = [
"David England",
"David France",
"David Spain",
"Roger Spain",
"Trevor England",
"Trevor Russia",
"Lucy Russia",
"Richard J. Russia",
"Richard J. England",
"Richard M. England",
];
$counts = [];
$countsWithMoreThanOneElement = [];
foreach ($names as $i => $name) {
if (trim($name) === '') {
continue;
}
$tmp = explode(' ', $name);
if (count($tmp) > 1) {
// Let's check for a dot or a single letter to separate location and names
$wordWithNames = [];
$foundMoreThanOneName = false;
foreach ($tmp as $j => $word) {
$wordWithNames[] = str_replace('.', '', $word);
if (strpos($word, '.') !== false || strlen($word) === 1) {
$foundMoreThanOneName = true;
break;
}
}
if (!$foundMoreThanOneName) {
array_pop($wordWithNames);
}
$name = implode(' ', $wordWithNames);
} else {
$name = $tmp[0];
}
if (!isset($counts[$name])) {
$counts[$name] = 0;
}
++$counts[$name];
if ($counts[$name] > 1 && !in_array($name, $countsWithMoreThanOneElement)) {
$countsWithMoreThanOneElement[] = $name;
}
}
print_r($countsWithMoreThanOneElement);
可运行示例:
编辑1:很抱歉,我第一次没有从字符串的开头读
编辑2:我又误解了问题的另一部分:p现在应该可以了 到目前为止,你尝试了什么?“从字符串的开头”是一个非常模糊的概念——Richard出现了3次,这有效吗?我编辑了我的答案,因为我第一次误解了你的问题。这就是你要找的吗?马丁,我最初开始数第一个单词,但当我意识到它可能不止第一个单词时,我被卡住了。@GlenUK你更改了输入值,但忘了根据它更改最终输出。是不是应该是
$result=[大卫英格兰,大卫法国巴黎,大卫,特雷弗,特雷弗俄罗斯,理查德J,理查德J英格兰,理查德M.]
$result = ["David", "Trever", "Richard J"]
<?php
$names = [
"David England",
"David France",
"David Spain",
"Roger Spain",
"Trevor England",
"Trevor Russia",
"Lucy Russia",
"Richard J. Russia",
"Richard J. England",
"Richard M. England",
];
$counts = [];
$countsWithMoreThanOneElement = [];
foreach ($names as $i => $name) {
if (trim($name) === '') {
continue;
}
$tmp = explode(' ', $name);
if (count($tmp) > 1) {
// Let's check for a dot or a single letter to separate location and names
$wordWithNames = [];
$foundMoreThanOneName = false;
foreach ($tmp as $j => $word) {
$wordWithNames[] = str_replace('.', '', $word);
if (strpos($word, '.') !== false || strlen($word) === 1) {
$foundMoreThanOneName = true;
break;
}
}
if (!$foundMoreThanOneName) {
array_pop($wordWithNames);
}
$name = implode(' ', $wordWithNames);
} else {
$name = $tmp[0];
}
if (!isset($counts[$name])) {
$counts[$name] = 0;
}
++$counts[$name];
if ($counts[$name] > 1 && !in_array($name, $countsWithMoreThanOneElement)) {
$countsWithMoreThanOneElement[] = $name;
}
}
print_r($countsWithMoreThanOneElement);
Array
(
[0] => David
[1] => Trevor
[2] => Richard J
)