How would I temporarily strip blocks of text and then reinsert after extra processing of the leftover?

I have the following text...

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lorem lacus, euismod ac convallis quis, adipiscing ut dui. [preformatted #1]This is preformatted text I do not want to alter[/preformatted] Ut porttitor Nunc urna dolor, porttitor vitae placerat sed, iaculis ut nibh. Etiam dignissim, nisl [preformatted #2]This is preformatted text I do not want to 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 to achieve is to replace all these preformatted blocks with temporary placeholder text such as [placeholder1], [placeholder2] and store the original blocks in an indexed array of some sort so the placeholders can be swapped back out for the originals after I have carried out some external processing on the block.

I would be extremely grateful if somebody could point me in the correct direction. Thanks in advance.


$blocks = array();

// gather preformatted blocks, and at the same time replace them in the
// original text with a 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 you do your processing on the $blocks array

// when done, put the blocks back in the text    
$str = strtr($str, $blocks);

For a proper and light BBcode parser try JBBCode

Note: I don't have PHP here so can't actually try this. Syntax may not be perfect.

Cleaner answer, as I now more fully understand your use case. First we accept an input string from a user, this will be a string but parts of it will be in this form [preformatted]some text[/preformatted]. We want to take that text and put it into an array using preg_match_all:

$input = $_POST['main_text'];
$preformatted = preg_match_all('/\[preformatted\](.*?)\[\/preformatted\]/is', $input);

Now we have the preformatted text strings in an array in the correct order, we replace them with placeholders like this (note - placeholders are going to be numbered, as I'm assuming whatever you do to this text may re-order the placeholders and we want to replace in the right order) using preg_replace:

$placeholders = array();
for ($i = 1; $i <= sizeof($preformatted); $i++) {
    $placeholders[$i] = '{PREFORMATTED'.$i.'}';
    preg_replace('/\[preformatted\](.*?)\[\/preformatted\]/', $placeholders[$i], $input, 1);

(We do this in a for loop limiting each iteration to one replacement, in order to increment the placeholder value. Because we know the number of replacements (sizeof($preformatted)), this is a good working solution.

Now we have an array of preformatted text strings ($preformatted), an array of placeholders ($placeholders) and a text string ready to have operations performed on it ($input).

Do whatever you want with the text, then finally switch the preformatted strings back in with str_replace:


Need Your Help

Breadth-first Resolution Algorithm

logic computer-science unification

I want to implement a resolution algorithm which tries to get empty set as it resolves the candidate clauses.

Query to WP db not working on regular PHP page

php wordpress

Wonder if anyone has any thoughts on this, I'm putting a Wordpress theme toegther - As it's very custom requirements and I'm using a gallery fro one part I put together from a combination of other

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.