如何使用preg_replace常见的反斜杠以十六进制为单词?

时间:2013-02-22 21:02:37

标签: php design-patterns preg-replace

我使用这种模式:

$line = preg_replace('#(\w)(\w)#e', "chr(hexdec('$1$2'))", $line);

但这对此字符串非常有用:"c8d3e320c7e1e1f8e520c7e1d1cde3e420c7e1d1cdede3fe"

我想收集字符串,例如: - "\'c8\'d3\'e3\'20\'c7\'e1\'e1\'f8\'e5\'20\'c7\'e1\'d1\'cd\'e3\'e4\'20\'c7\'e1\'d1\'cd\'ed\'e3\'fe"

这意味着以\'开头的任何字符 如何改变模式?

谢谢+ Akam

这种模式解决了问题

  $line = preg_replace("#\\\'(\w)(\w)#e", "chr(hexdec('$1$2'))", $line);

1 个答案:

答案 0 :(得分:0)

$line = "\'c8\'d3\'e3\'20\'c7\'e1\'e1\'f8\'e5\'20\'c7\'e1\'d1\'cd\'e3\'e4\'20\'c7\'e1\'d1\'cd\'ed\'e3\'fe";
echo preg_replace("#\\'(\w)(\w)#e", "chr(hexdec('$1$2'))", $line);

//test
preg_match_all("#\\'(\w)(\w)#e", $line, $out);
echo '<pre>';
print_r($out);

    [1] => Array
        (
            [0] => c
            [1] => d
            [2] => e
            [3] => 2
            [4] => c
            [5] => e
            [6] => e
            [7] => f
            [8] => e
            [9] => 2
            [10] => c
            [11] => e
            [12] => d
            [13] => c
            [14] => e
            [15] => e
            [16] => 2
            [17] => c
            [18] => e
            [19] => d
            [20] => c
            [21] => e
            [22] => e
            [23] => f
        )

    [2] => Array
        (
            [0] => 8
            [1] => 3
            [2] => 3
            [3] => 0
            [4] => 7
            [5] => 1
            [6] => 1
            [7] => 8
            [8] => 5
            [9] => 0
            [10] => 7
            [11] => 1
            [12] => 1
            [13] => d
            [14] => 3
            [15] => 4
            [16] => 0
            [17] => 7
            [18] => 1
            [19] => 1
            [20] => d
            [21] => d
            [22] => 3
            [23] => e
        )

)