preg_replace 如何用" 包围字符串的html 属性在 PHP 中 [英] preg_replace how surround html attributes for a string with " in PHP
本文介绍了preg_replace 如何用" 包围字符串的html 属性在 PHP 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 PHP 中有一个字符串变量,它的内容是:
I have a string variable in PHP , its content is:
$var='<SPAN id=1 value=1 name=1> one</SPAN>
<div id=2 value=2 name=2> two</div >';
....
我需要一个用 "" 来包围 html 属性的函数,我需要为所有元标记
等执行此操作,结果应该是这样的:
I need a function for surround html attributes with "" i need do this for the all meta-tag
,etc the result should be this:
$var='<SPAN id= "1" value="1" name="1"> one </SPAN>
<div id="2" value="2" name="2" > two</div >';
...
我需要将所有 =[a-z][A-Z][1-9] 替换为 ="[a-z][A-Z][1-9]".我需要 preg_replace 的正则表达式
I need replace all =[a-z][A-Z][1-9] for ="[a-z][A-Z][1-9]". I need a regular expresion for preg_replace
推荐答案
Its is 解决办法
$var = preg_replace('/(?<==)(\b\w+\b)(?!")(?=[^<]*>)/', '"$1"', $var);
感谢 Ωmega,它适用于 IE8
thanks for Ωmega, its works on IE8
这篇关于preg_replace 如何用" 包围字符串的html 属性在 PHP 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文