http://www.perlmonks.org?node_id=183098

epoptai has asked for the wisdom of the Perl Monks concerning the following question:

I've got a string with text and html tags destined for a process that will damage the html. So I want to temporarily replace the html with placeholders while storing a "placeholder => html" map in a hash, so it can be restored after the destructive process.

As an example I need to turn this:

$str = qq~This <b>contains</b> both text and <a href="http://www.w3c.org">ht +ml</a>.~;
Into this string and hash:
$str = qq~This <1>contains<2> both text and <3>html<4>.~; $html = { 1 => 'b', 2 => '/b', 3 => 'a href="http://www.w3c.org"', 4 => '/a' }
Then I can process the string and restore the html with something like:
$str =~ s|<(\d+)>|<$$html{$1}>|g;
I thought this would be easy, but I'm still staring at the wall. Thanks.

--
Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.