【发布时间】: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