正则表达式(在PHP中)以匹配&不是HTML实体 [英] regex (in PHP) to match & that aren't HTML entities

查看:181
本文介绍了正则表达式(在PHP中)以匹配&不是HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是:用& amp; amp;替换所有独立的&字符.但不能替换那些已经属于HTML实体(例如& nbsp;)的对象.

Here's the goal: to replace all standalone ampersands with & but NOT replace those that are already part of an HTML entity such as  .

我认为我需要一个只与独立的&符号匹配的PHP正则表达式(最好是preg_函数).我只是不知道如何使用preg_replace.

I think I need a regular expression for PHP (preferably for preg_ functions) that will match only standalone ampersands. I just don't know how to do that with preg_replace.

推荐答案

您始终可以在运行htmlentities之前先运行html_entity_decode吗?除非您只想执行&符号(否则即使您可以使用charset参数也可以使用),否则该方法将起作用.

You could always run html_entity_decode before you run htmlentities? Works unless you only want to do ampersands (and even then you can play with the charset parameters).

比正则表达式更容易,更快捷.

Much easier and faster than a regex.

这篇关于正则表达式(在PHP中)以匹配&不是HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆