Preg_replace用于编码

时间:2014-02-18 12:40:01

标签: php regex

我在正则表达式中非常糟糕,

我在PHP的现有应用程序中看到了以下代码,但并不真正理解它在做什么。

$content = preg_replace('/(<?xml[^>]*)encoding=["\']([^>"\']*)?["\']([^>]*?>)/', '$1encoding="' . $encoding . '"$3', $content);

        return $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);

更新

这可能听起来很愚蠢..但它正在工作......因为它是XML标签,所以我需要在chrome中显示源并显示它...

谢谢大家..特别是M42

的问候, 莫纳

1 个答案:

答案 0 :(得分:2)

它将通过变量$encoding

中包含的新编码更改文件中存在的编码

示例:

 <?xml blah blah encode="XXX" blah blah?>

将更改为(假设$encoding='UTF-8';):

 <?xml blah blah encode="UTF-8" blah blah?>

$1包含:<xml blah blah
$3包含:blah blah>
结果是$1encode="UTF-8"和$ 3的连接。

正则表达式解释:

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
----------------------------------------------------------------------