【发布时间】:2013-03-22 04:06:29
【问题描述】:
我需要解析一个 HTML 文档并在其中找到所有出现的字符串 asdf。
我目前已将 HTML 加载到字符串变量中。我只想要字符位置,这样我就可以遍历列表以在字符串之后返回一些数据。
strpos 函数只返回 first 出现。 全部返回怎么样?
【问题讨论】:
我需要解析一个 HTML 文档并在其中找到所有出现的字符串 asdf。
我目前已将 HTML 加载到字符串变量中。我只想要字符位置,这样我就可以遍历列表以在字符串之后返回一些数据。
strpos 函数只返回 first 出现。 全部返回怎么样?
【问题讨论】:
不使用正则表达式,这样的东西应该可以返回字符串位置:
$html = "dddasdfdddasdffff";
$needle = "asdf";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
// Displays 3 and 10
foreach ($positions as $value) {
echo $value ."<br />";
}
【讨论】:
if 语句中使用赋值。在这种情况下,您的 while 循环不适用于位置 0。我已经更新了你的答案。
您可以重复调用strpos 函数,直到找不到匹配项。您必须指定偏移参数。
注意:在以下示例中,搜索从 下一个 字符开始,而不是从上一个匹配项的结尾开始。根据这个函数,aaaa 包含 三个 出现的子字符串 aa,而不是两个。
function strpos_all($haystack, $needle) {
$offset = 0;
$allpos = array();
while (($pos = strpos($haystack, $needle, $offset)) !== FALSE) {
$offset = $pos + 1;
$allpos[] = $pos;
}
return $allpos;
}
print_r(strpos_all("aaa bbb aaa bbb aaa bbb", "aa"));
输出:
Array
(
[0] => 0
[1] => 1
[2] => 8
[3] => 9
[4] => 16
[5] => 17
)
【讨论】:
最好使用substr_count。查看php.net
【讨论】:
function getocurence($chaine,$rechercher)
{
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($chaine, $rechercher, $lastPos))!== false)
{
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($rechercher);
}
return $positions;
}
【讨论】:
【讨论】:
这可以使用 strpos() 函数来完成。下面的代码是使用 for 循环实现的。这段代码非常简单而且非常直接。
<?php
$str_test = "Hello World! welcome to php";
$count = 0;
$find = "o";
$positions = array();
for($i = 0; $i<strlen($str_test); $i++)
{
$pos = strpos($str_test, $find, $count);
if($pos == $count){
$positions[] = $pos;
}
$count++;
}
foreach ($positions as $value) {
echo '<br/>' . $value . "<br />";
}
?>
【讨论】:
Salman A 有一个很好的答案,但请记住让您的代码具有多字节安全性。要使用 UTF-8 获得正确的位置,请使用 mb_strpos 而不是 strpos:
function strpos_all($haystack, $needle) {
$offset = 0;
$allpos = array();
while (($pos = mb_strpos($haystack, $needle, $offset)) !== FALSE) {
$offset = $pos + 1;
$allpos[] = $pos;
}
return $allpos;
}
print_r(strpos_all("aaa bbb aaa bbb aaa bbb", "aa"));
【讨论】:
简单的strpos_all()函数。
function strpos_all($haystack, $needle_regex)
{
preg_match_all('/' . $needle_regex . '/', $haystack, $matches, PREG_OFFSET_CAPTURE);
return array_map(function ($v) {
return $v[1];
}, $matches[0]);
}
用法: 像针一样简单的绳子。
$html = "dddasdfdddasdffff";
$needle = "asdf";
$all_positions = strpos_all($html, $needle);
var_dump($all_positions);
输出:
array(2) {
[0]=>
int(3)
[1]=>
int(10)
}
或者用正则表达式作为针。
$html = "dddasdfdddasdffff";
$needle = "[d]{3}";
$all_positions = strpos_all($html, $needle);
var_dump($all_positions);
输出:
array(2) {
[0]=>
int(0)
[1]=>
int(7)
}
【讨论】:
strpos 会简单得多,并且可以完成工作。
<?php
$mainString = "dddjmnpfdddjmnpffff";
$needle = "jmnp";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
// Displays 3 and 10
foreach ($positions as $value) {
echo $value ."<br />";
}
?>
【讨论】:
另一种解决方案是使用explode():
public static function allSubStrPos($str, $del)
{
$searchArray = explode($del, $str);
unset($searchArray[count($searchArray) - 1]);
$positionsArray = [];
$index = 0;
foreach ($searchArray as $i => $s) {
array_push($positionsArray, strlen($s) + $index);
$index += strlen($s) + strlen($del);
}
return $positionsArray;
}
【讨论】: