即使存在正则表达式模式,PHP preg_match也会失败

rc

我要复制此面包屑数组,每个面包屑都减去面包屑的最后一项。

我尝试过的。我已经尝试过preg_match使用模式“/.*(?=\s»\s)/”,该模式应该可以在的最后一个实例之前获取所有内容»这是我尝试的解决方案:

<?php
$breadcrumb = array (
  'More » Brackets & Mounting » StarTech MNRISERCLMP',
  'Printer Consumables » Toner Cartridges » Other Brand » Sharp MX27GTMA',
  'More » Audio Visual » TVs » Philips 43BDL4051T-EXG',
  'Servers » Server Cables & Accessories » Other Server Accessories » LINKBASIC LB-WCC09-655-CA'
);



foreach($breadcrumb as $breadcrumbs){
        preg_match("/.*(?=\s»\s)/",$breadcrumbs,$matches);
        echo $matches[1] . '<br />';
    }

给出:


警告:未定义阵列键1 [...] [...]在线路13


警告:未定义阵列键1 [...] [...]在线路13


警告:在未定义阵列键1 [.. ] [...]在线路13


警告:未定义阵列键1 [...] [...]上线13

我知道错误告诉我没有匹配项,但从逻辑上讲我看不到preg_match失败的原因。

用户14717238

实际上,您的结果将在$matches[0]

echo $matches[0] . '<br />';

或尝试以下模式:

preg_match("/(.+)\s»\s.+$/", $breadcrumbs, $matches);
echo $matches[1] . '<br />';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP正则表达式转义字符错误:“ preg_match():编译失败:缺少”

PHP 中的正则表达式与 preg_match

PHP preg_match和正则表达式

PHP preg_match()正则表达式模式与空格匹配的问题

PHP preg_match使用正则表达式的简单数学表达式

preg_match 语句中的正则表达式

正则表达式和preg_match

preg_match的正则表达式

PHP preg_match与表达式的模式不匹配

使用 preg_match 和正则表达式进行 PHP 表单验证

PHP preg_match使用URL作为正则表达式

php preg_match与正则表达式无法正常工作

奇怪的 PHP 正则表达式 Preg_Match 错误?

正则表达式,用于以空格分隔的输入要在php的preg_match中使用

php preg_match问题,带有“ [image:内嵌图像1]”正则表达式

php preg_match和regex正则表达式

正则表达式php中的Preg_match替代值

php,正则表达式preg_match特定单词0或1次

preg_match_all PHP的条件正则表达式模式

PHP中的正则表达式返回preg_match():编译失败:PCRE不支持\ L,\ l,\ N {name},\ U或\ u

PHP正则表达式失败

PHP正则表达式失败

PHP preg_match_all正则表达式

preg_match中的正则表达式不匹配行注释源代码并捕获错误的子模式

即使涉及正则表达式,当涉及到正则表达式时,无法转义也会失败

php和apache更新后Unicode表达式在preg_match中失败

php:preg_match失败,出现特殊字符模式

Preg Match正则表达式

使用正则表达式模式的preg_replace