【发布时间】:2014-11-27 00:54:30
【问题描述】:
我正在尝试使用 preg_split 拆分字符串,获取结果,用自定义数据包围它们,然后将它们重新插入到原始字符串中。
例如:
$string="this is text [shortcode] this is also text [shortcode2] this is text";
之后,我希望字符串等于:
$string="<span id='0'>this is text </span>[shortcode]<span id='1'> this is also text </span>[shortcode2]<span id='2'>this is text</span>";
我成功地将字符串拆分为一个数组:
$array=preg_split(/\[[^\]]*\]/,$string);
然后我尝试了一个 for next 循环来替换值 - 效果很好,除非数组值在字符串中具有相同的匹配项。
感谢任何帮助 - 或其他可能更好的方法。
【问题讨论】:
标签: php regex string preg-split