php 在字符串中查找多次出现的字符串
php find multiple occurence of string in string
我有一个基本上可以检索描述中的产品 ID 的功能。
private function scanForProductIdInDescription($string, $start, $end) {
$startpos = strpos($string, $start) + strlen($start);
if (strpos($string, $start) !== false) {
$endpos = strpos($string, $end, $startpos);
if (strpos($string, $end, $startpos) !== false) {
return substr($string, $startpos, $endpos - $startpos);
}
}
}
我使用它如下:
$from = "{PID =";
$end = "}";
$description = 'some text {PID =340} {PID =357}';
$product_id = $this->scanForProductIdInDescription($description, $from, $end);
目前,它只在字符串中第一次出现。我需要找到字符串中所有出现的地方。结果应该是:
$product_id = 340,357;
谢谢
使用正则表达式而不是 strpos()
是您最好的选择。我已经快速将以下内容与您的示例一起使用;
\{PID\s=([0-9]*)\}
You can see a working version here
在 PHP 中使用它看起来像;
$re = '/\{PID\s=([0-9]*)\}/';
$str = 'some text {PID =340} {PID =357}';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
编辑:编辑为 return 仅匹配字符串中的实际 ID。 IMO - 这是比发布的其他 2 个答案更好的解决方案,因为它 returns ID 的任何长度,并且只有 returns ID 与您提供的格式匹配。
我也更新了我的工作示例。
您可以使用 preg_match_all:
$description = 'some text {PID =340} {PID =357}';
preg_match_all('/=([0-9]+)\}/', $description, $matches);
var_dump($matches);
结果是:
array(2) {
[0]=>
array(2) {
[0]=>
string(5) "=340}"
[1]=>
string(5) "=357}"
}
[1]=>
array(2) {
[0]=>
string(3) "340"
[1]=>
string(3) "357"
}
}
您可以使用 preg_match_all 如下:
<?php
// $sPattern = '^{PID\s=\d{3}}^';
// by using a capture group "(" + ")" to enclose the number (\d), you can output just the numbers
$sPattern = '{PID\s=(\d{3})}';
$aOutput = array();
$sString = 'some text {PID =340} {PID =357}';
preg_match_all($sPattern, $sString, $aOutput);
// implode the first capture group to a string
$aOutput = implode(",", $aOutput[1]);
echo "<pre>";
var_dump($aOutput);
?>
这将输出:
string(7) "340,357"
要获得所需的结果(仅 PID 数字)- 使用以下方法:
$description = 'some text {PID =340} {PID =357}';
preg_match_all("/(?<=\{PID =)\d+(?=\})/", $description, $matches);
$result = $matches[0];
print_r($result);
输出:
Array
(
[0] => 340
[1] => 357
)
您可以完全避免捕获组,方法是使用 \K
在您想要的数字子字符串之前重新开始全字符串匹配。
代码:(演示:https://3v4l.org/WalUc)
$description = 'some text {PID =340} {PID =357}';
echo implode(',', preg_match_all('~\{PID =\K\d+~', $description, $out) ? $out[0] : []);
输出:
340,357
这项技术的好处是:
- 正则表达式引擎生成的步骤更少。
- 匹配数组中没有不必要的膨胀 (
$out
)。
我有一个基本上可以检索描述中的产品 ID 的功能。
private function scanForProductIdInDescription($string, $start, $end) {
$startpos = strpos($string, $start) + strlen($start);
if (strpos($string, $start) !== false) {
$endpos = strpos($string, $end, $startpos);
if (strpos($string, $end, $startpos) !== false) {
return substr($string, $startpos, $endpos - $startpos);
}
}
}
我使用它如下:
$from = "{PID =";
$end = "}";
$description = 'some text {PID =340} {PID =357}';
$product_id = $this->scanForProductIdInDescription($description, $from, $end);
目前,它只在字符串中第一次出现。我需要找到字符串中所有出现的地方。结果应该是: $product_id = 340,357;
谢谢
使用正则表达式而不是 strpos()
是您最好的选择。我已经快速将以下内容与您的示例一起使用;
\{PID\s=([0-9]*)\}
You can see a working version here
在 PHP 中使用它看起来像;
$re = '/\{PID\s=([0-9]*)\}/';
$str = 'some text {PID =340} {PID =357}';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
编辑:编辑为 return 仅匹配字符串中的实际 ID。 IMO - 这是比发布的其他 2 个答案更好的解决方案,因为它 returns ID 的任何长度,并且只有 returns ID 与您提供的格式匹配。
我也更新了我的工作示例。
您可以使用 preg_match_all:
$description = 'some text {PID =340} {PID =357}';
preg_match_all('/=([0-9]+)\}/', $description, $matches);
var_dump($matches);
结果是:
array(2) {
[0]=>
array(2) {
[0]=>
string(5) "=340}"
[1]=>
string(5) "=357}"
}
[1]=>
array(2) {
[0]=>
string(3) "340"
[1]=>
string(3) "357"
}
}
您可以使用 preg_match_all 如下:
<?php
// $sPattern = '^{PID\s=\d{3}}^';
// by using a capture group "(" + ")" to enclose the number (\d), you can output just the numbers
$sPattern = '{PID\s=(\d{3})}';
$aOutput = array();
$sString = 'some text {PID =340} {PID =357}';
preg_match_all($sPattern, $sString, $aOutput);
// implode the first capture group to a string
$aOutput = implode(",", $aOutput[1]);
echo "<pre>";
var_dump($aOutput);
?>
这将输出:
string(7) "340,357"
要获得所需的结果(仅 PID 数字)- 使用以下方法:
$description = 'some text {PID =340} {PID =357}';
preg_match_all("/(?<=\{PID =)\d+(?=\})/", $description, $matches);
$result = $matches[0];
print_r($result);
输出:
Array
(
[0] => 340
[1] => 357
)
您可以完全避免捕获组,方法是使用 \K
在您想要的数字子字符串之前重新开始全字符串匹配。
代码:(演示:https://3v4l.org/WalUc)
$description = 'some text {PID =340} {PID =357}';
echo implode(',', preg_match_all('~\{PID =\K\d+~', $description, $out) ? $out[0] : []);
输出:
340,357
这项技术的好处是:
- 正则表达式引擎生成的步骤更少。
- 匹配数组中没有不必要的膨胀 (
$out
)。