Php 如果字符串出现10次,则在10日运行函数
我使用php的get_Contents()函数从文件中获取字符串 但是,在继续进行需要执行的操作之前,我需要找到一个字符串是否有10个实例,在本例中是Php 如果字符串出现10次,则在10日运行函数,php,Php,我使用php的get_Contents()函数从文件中获取字符串 但是,在继续进行需要执行的操作之前,我需要找到一个字符串是否有10个实例,在本例中是,然后为该特定部分运行一个函数 我认为在得到最后一个后,以下是可能的: $str_itemOpen = /* get 10th <item> in list */ $str_itemClose = /* get 10th </item> in list */ $str = $str_itemOpen . $str_itemC
,然后为该特定部分运行一个函数
我认为在得到最后一个后,以下是可能的:
$str_itemOpen = /* get 10th <item> in list */
$str_itemClose = /* get 10th </item> in list */
$str = $str_itemOpen . $str_itemClose
$removed_last = preg_replace(/'.'/, '', $str);
$str_itemOpen=/*排名第十*/
$str_itemClose=/*排名第十*/
$str=$str\u itemOpen$str_itemClose
$removed_last=preg_replace(/'./,'','',$str);
我不是100%确定这会起作用,但基本上它将在文件中第10次出现
和
,如果它们存在,然后删除标记和其中的所有内容。下面的代码将获得
和
的所有位置
查看字符串可能会很有用。如果是XML/HTML,则可以使用解析器获得更好的结果。
$needle = "<item>";
$lastPos = 0;
$positionsItemOpen = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positionsItemOpen[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
$needle = "</item>";
$lastPos = 0;
$positionsItemClose = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positionsItemClose[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
if (count($positionsItemOpen) >= 10)
{
//there are at least 10 instances, so you can work with it
//do the manipulations with string
$itemLength = $positionsItemClose[9] - $positionsItemOpen[9];
$itemStr = substr($str, $positionsItemOpen[9], $itemLength);
$removed_last = preg_replace(/'.'/, '', $str);
}