【问题标题】:Preg_replace replace specific phrasepreg_replace 替换特定短语
【发布时间】:2013-09-03 15:53:20
【问题描述】:

我有一个描述,其中包含我要删除的两段文本和要格式化的文本。该字符串可以更改并且其中包含  和空格(这是错误数据)。

我需要同时删除产品名称和末尾的一段文本(两者都可以根据“产品”进行更改)。我将产品名称存储为如下变量:

$prName = "Test CPL560 Home Office Corner Workstation";

需要删除的末尾文本如下:

free delivery5-7 working days  assembly required?Yes   guarantee2 years  dimensions(mm) Width:  1600-2340  Depth:  700-1350  Height: 760

删除产品名称应该相当简单,但最后一段文字让我很困惑,因为宽度、深度等都可能因产品而异。有什么建议吗?

完整字符串:

Test CPL560 Home Office Corner WorkstationExecutive, designer style corner workstation. The smart-looking, functional CPL560 with complementing side storage unit featuring high gloss black drawer fronts, provides the perfect solution for the modern, spacious home office. Quality 32mm desktop measuring 1600x700mm, finished with  2mm ABS edge protection providing a comfortable, durable finish. The desktop rear wall features cable guide and rear cable housing. The side return storage unit of the Test CPL560 corner computer desk rotates through 360 degrees to allow the side return to be located and fixed on either the right or left hand side of the desk top, whichever best suits your working position and home office layout. The side return unit comprises 3 push-to-open storage drawers with high gloss drawer fronts, a CPU storage compartment with rear cable management and 2 further storage compartments with one height adjustable shelf. CPL560 Corner Workstation is available in White/High Gloss BlackOptional installation/assembly available for this product  free delivery5-7 working days  assembly required?Yes   guarantee2 years  dimensions(mm) Width:  1600-2340  Depth:  700-1350  Height: 760

我想要什么:

测试 CPL560 家庭办公室角落工作站行政,设计师风格的角落工作站。外观时尚、功能齐全的 CPL560 带有互补的侧储物单元,具有高光黑色抽屉正面,为现代、宽敞的家庭办公室提供了完美的解决方案。优质 32 毫米桌面尺寸为 1600x700 毫米,采用 2 毫米 ABS 边缘保护,提供舒适、耐用的饰面。桌面后壁具有电缆引导器和后电缆外壳。

Test CPL560 转角电脑桌的侧边回位存储单元可 360 度旋转,以便将侧边回位定位并固定在桌面的右侧或左侧,以最适合您的工作位置和家庭的为准办公室布局。侧面返回单元包括 3 个带高光泽抽屉正面的推开式储物抽屉、一个带后部电缆管理的 CPU 储物箱和 2 个带一个高度可调节搁板的其他储物箱。 CPL560 转角工作站有白色/高光黑色可供此产品选择安装/组装

我不关心分段发生在哪里,只是为了让它半可读。这可以通过 preg_replace 或 regex 实现吗?

【问题讨论】:

  • 创建时不能清理吗? IE 如果来自数据库,使用实体等。只是一个想法。如果可能,产生好的结果比清理坏的结果更好
  • 我完全同意詹姆斯,但是这将是一个太好的想法来实现(如果你明白我的意思!)。我无法访问或控制源数据库或数据,只有可怜的草皮必须清理它。如果我一开始就接触到它,这将永远不会发生。
  • 我认为为上下文解析英语有点困难。它可能无法撤消。

标签: php regex string preg-replace data-manipulation


【解决方案1】:

你可以试试这个:

$result = str_replace(' ', ' ', $text);
$result = str_replace($prName, '', $result);
$result = preg_replace('~^.*?yreviled eerf +~', '', strrev($result));
$result = strrev($result);

【讨论】:

  • 为什么要采取额外的反转字符串的步骤? “免费送货5.+$”有什么问题
  • 看来,如果担心多次“免费送货”,只需将整个排除字符串与结束锚点放在一起。 strcmp 总是比字符串反转快。引擎将进行完全匹配。
【解决方案2】:

如果您知道所有可能存在的“结束文本”场景,您应该可以多次执行此操作。 首先,您可以将  转换为空格

$string = str_replace(' ', ' ', $string);

$string = html_entity_decode($string, ...);

然后,您可以使用 explode(' ', $string); 对单词进行标记以获得大量单词,然后遍历单词以检测精确匹配,例如 Width:Height:free delivery,以将它们剥离成一个属性大批。用implode(' ', $words);重新加入主要描述

【讨论】:

    【解决方案3】:

    文本中的值可以更改的模式,基于正则表达式的搜索和替换可以做到。

    但在此之前我会规范化文本,例如将  替换为空格,两个或多个空格替换为一个空格等。但只有当您不需要“污垢”来确定要删除哪些部分时才执行该步骤。在这种情况下,先删除,然后规范化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 2014-06-10
      • 1970-01-01
      相关资源
      最近更新 更多