Php 用于编码的Preg_替换
我的正则表达式很差 下面是我在php的现有应用程序中看到的代码,但我并不真正理解它在做什么Php 用于编码的Preg_替换,php,regex,Php,Regex,我的正则表达式很差 下面是我在php的现有应用程序中看到的代码,但我并不真正理解它在做什么 $content = preg_replace('/(<?xml[^>]*)encoding=["\']([^>"\']*)?["\']([^>]*?>)/', '$1encoding="' . $encoding . '"$3', $content); return $content; $content=preg\u replace('/(]*)enco
$content = preg_replace('/(<?xml[^>]*)encoding=["\']([^>"\']*)?["\']([^>]*?>)/', '$1encoding="' . $encoding . '"$3', $content);
return $content;
$content=preg\u replace('/(]*)encoding=[“\”]([^>“\”]*)?[“\”]([^>]*?>)/”,“$1encoding=“”.$encoding.”“$3”,$content);
返回$content;
如果有专家可以分享一些信息,那将是非常棒的
更新
试着运行但不起作用我缺少了什么
$encoding = 'UTF-32';
$content = '<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel></channel>';
echo preg_replace('/(<?xml[^>]*)encoding=["\']([^>"\']*)?["\']([^>]*?>)/', '$1encoding="' . $encoding . '"$3', $content);
$encoding='UTF-32';
$content='1
';
echo preg\u replace(“/(]*)编码=[“\”]([^>“\”]*)?[“\”]([^>]*?>)/”,“$1encoding=“”。$encoding.”“$3”,$content);
更新
这听起来可能很愚蠢。。但它起作用了。。。因为它是XML标记,所以我需要在chrome中显示源代码,它将显示它
谢谢大家。。特别是M42
问候,,
Mona它将通过变量
$encoding
例如:
<?xml blah blah encode="XXX" blah blah?>
$1
包含:
结果是,
$1
、encode=“UTF-8”
和$3的串联
正则表达式解释:
正则表达式:
(]*)编码=[“\”]([^>“\”]*)?[“\”]([^>]*?>)
匹配项如下:
节点解释
----------------------------------------------------------------------
(组和捕获到\1:
----------------------------------------------------------------------
]*除“>”(0或更多)以外的任何字符
次数(与最大金额匹配)
(可能的)
----------------------------------------------------------------------
)结束\1
----------------------------------------------------------------------
encoding='encoding='
----------------------------------------------------------------------
[“\”]以下任意字符:“,“\”
----------------------------------------------------------------------
(分组并捕获到\2(可选)
(匹配尽可能多的金额):
----------------------------------------------------------------------
[^>“\”]*除以下字符以外的任何字符:“>”、“”、“\”(0
或更多次(与最大金额匹配)
(可能的)
----------------------------------------------------------------------
)?结束\2(注意:因为您正在使用
量词在此捕获,只有最后一个
捕获模式的重复将被删除
存储在\2)中
----------------------------------------------------------------------
[“\”]以下任意字符:“,“\”
----------------------------------------------------------------------
(分组并捕获到\3:
----------------------------------------------------------------------
[^>]*?除“>”(0或更多)以外的任何字符
次数(匹配最小金额)
(可能的)
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
)结束\3
----------------------------------------------------------------------
谢谢你M42..$encoding=UTF-8,$content=所有XML数据,但我不明白这里的$1编码和$3是什么,你有什么想法吗…如果你能举个例子的话..提前谢谢你。th aviable没有th变量;)很棒的M42..谢谢你,它帮了很多忙。尝试执行它看起来有点问题..不使用$encoding='UTF-32';$content='';echo preg\u replace('/(]*)encoding=[“\']([^>“\']*)?[“\']([^>]*?>)/”,“$1编码”“。$encoding.”,$3',$content);
<?xml blah blah encode="UTF-8" blah blah?>
The regular expression:
(<?xml[^>]*)encoding=["\']([^>"\']*)?["\']([^>]*?>)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
<? '<' (optional (matching the most amount
possible))
----------------------------------------------------------------------
xml 'xml'
----------------------------------------------------------------------
[^>]* any character except: '>' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
encoding= 'encoding='
----------------------------------------------------------------------
["\'] any character of: '"', '\''
----------------------------------------------------------------------
( group and capture to \2 (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[^>"\']* any character except: '>', '"', '\'' (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
)? end of \2 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \2)
----------------------------------------------------------------------
["\'] any character of: '"', '\''
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[^>]*? any character except: '>' (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------