Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP替换/删除文本和数字_Php_Regex_Replace_Preg Replace - Fatal编程技术网

使用PHP替换/删除文本和数字

使用PHP替换/删除文本和数字,php,regex,replace,preg-replace,Php,Regex,Replace,Preg Replace,我不知道如何删除以下内容: 第页项目第页项目xxx xxx=一个数字 例如: 我想要这个: <?php $r='some text before page_item page-item-123 some text after'; $r=preg_replace('page_item page-item-[WHAT] ','',$r); echo $r; ?> 回馈: <?php $r='some text before page_item page-item-1

我不知道如何删除以下内容:

第页项目第页项目xxx

xxx=一个数字

例如:

我想要这个:

<?php

$r='some text before page_item page-item-123 some text after';

$r=preg_replace('page_item page-item-[WHAT] ','',$r);

echo $r;

?>

回馈:

<?php

$r='some text before page_item page-item-123 some text after';

$r=preg_replace('page_item page-item-[WHAT] ','',$r);

echo $r;

?>

这是一个非常简单的正则表达式。使用
\d+
匹配末尾的一个或多个数字。不要忘记数字组后面的空格,这样输出字符串中就不会出现两个连续的空格

$r = preg_replace('/page_item page-item-(\d+) / ','',$r);
echo $r;
// some text before some text after

请尽量更具体地回答您的问题--很难准确理解您是如何修改原始字符串的。
/(page\u item)\s(page item-\d\d\d)/
谢谢Michael,这很简单:)