php - How would I temporarily strip blocks of text and then reinsert after extra processing of the leftover? -
i have following text...
lorem ipsum dolor sit amet, consectetur adipiscing elit. cras lorem lacus, euismod ac convallis quis, adipiscing ut dui.
[preformatted #1]this preformatted text not want alter[/preformatted]ut porttitor nunc urna dolor, porttitor vitae placerat sed, iaculis ut nibh. etiam dignissim, nisl[preformatted #2]this preformatted text not want alter[/preformatted]commodo pulvinar facilisis, eros enim volutpat ante, sed feugiat risus justo vitae ipsum. duis lobortis hendrerit orci, non semper dolor porta sed.
what i'm trying achieve replace these preformatted blocks temporary placeholder text such [placeholder1], [placeholder2] , store original blocks in indexed array of sort placeholders can swapped out originals after have carried out external processing on block.
i extremely grateful if point me in correct direction. in advance.
$blocks = array(); // gather preformatted blocks, , @ same time replace them in // original text hash $str = preg_replace_callback('/\[preformatted #(\d+)\](.+?)\[\/preformatted\]/is', function($match) use(&$blocks){ $hash = '<!-- ' . md5($match[2]) .' -->'; $blocks[$hash] = $match[2]; return $hash; }, $str); // here processing on $blocks array // when done, put blocks in text $str = strtr($str, $blocks); for proper , light bbcode parser try jbbcode
Comments
Post a Comment