【问题标题】:preg_replace is not working with special charspreg_replace 不适用于特殊字符
【发布时间】:2016-03-03 10:24:59
【问题描述】:

我正在尝试使用 stackoverflow 答案中的函数修复一些错误序列化的数据。我意识到一些特殊的字符正在打破这种模式。有没有办法解决这个问题?

字符串:

s:4:"text";s:317:"<div style="text-align:center">
<img src="http://dev-xxxxxxx.yyyyyyy.net/mirza/xxxxxxxx/yyyyyyyy/wp-content/uploads/sites/13/2014/09/footer-logo1.png" alt="yyyyyyyy template" />
</div>
<div style="text-align:center;margin-top:30px"><span style="font-size:11px;color:#fff">COPYRIGHT ALL RIGHTS RESERVED � 2014</span></div>

";s:6:"filter";b:0;}s:12:"_multiwidget";

功能:

    $row_sub["option_value"] = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) {

        echo " - ".$match[1]." -
        ";
        echo " - ".strlen($match[2])." - <br />
        ";
        echo " - ".$match[2]." - <br />
        ";

        return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
    },$row_sub["option_value"]);

输出:

                     - 4 -
                     - 4 - <br />
                     - text - <br />
                     - 6 -
                     - 6 - <br />
                     - filter - <br />
                     - 12 -
                     - 12 - <br />
                     - _multiwidget - <br />

预期输出:

                 - 4 -
                 - 4 - <br />
                 - text - <br />
                 - 317 - 
                 - 325 - <br />
                 -  <div style="text-align:center">
                            <img src="http://dev-xxxxxxx.yyyyyyy.net/mirza/xxxxxxxx/yyyyyyyy/wp-content/uploads/sites/13/2014/09/footer-logo1.png" alt="yyyyyyyy template" />
                            </div>
                            <div style="text-align:center;margin-top:30px"><span style="font-size:11px;color:#fff">COPYRIGHT ALL RIGHTS RESERVED � 2014</span></div> - <br /> 
                 - 6 -
                 - 6 - <br />
                 - filter - <br />
                 - 12 -
                 - 12 - <br />
                 - _multiwidget - <br />

如您所见,缺少带有 html 标记的部分。可能是因为版权标志。

我该如何解决这个问题?

【问题讨论】:

  • 你能添加你想要的输出吗?
  • @LouisBarranqueiro 添加。

标签: php regex preg-replace regex-greedy preg-replace-callback


【解决方案1】:

这不是一个特殊的字符问题。

使用适当的分隔符解决了这个问题:

preg_replace_callback ( '!s:(\d+):"(.*?)";!smi',

【讨论】:

  • 你的意思是//s 修饰符(全点)就足够了吗?
  • 那么你不需要多行或不区分大小写的修饰符。但我可以理解把厨房水槽扔在它上面。
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多