【问题标题】:Replace/remove text and numbers using PHP使用 PHP 替换/删除文本和数字
【发布时间】:2011-12-07 04:02:12
【问题描述】:

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

page_item page-item-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;

?>

回馈:

一些文本之前的一些文本之后的一些文本

【问题讨论】:

  • 请尝试更具体地回答您的问题 - 很难准确理解您是如何尝试修改原始字符串的。
  • /(page_item)\s(page-item-\d\d\d)/

标签: php regex replace preg-replace


【解决方案1】:

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

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

【讨论】:

  • 谢谢迈克尔,这很简单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多