Jump to content

Replace contents of HTML tag with something else


MCrosbie

Recommended Posts

Hi there,

 

I have the following tag, which I want the contents of which to be hidden. This is a email that is being emailed to two people, one who is allowed to view the price, one who isn't.

 

This is what I have at present:

<price>$ 10.99</price>

 

(Prices will change)

 

I want this to be replaced with

<price>$ --.--</price>

 

Any ideas?

 

Cheers, Michael

If this is XML, I recommend building a filter on top of an Indentity Transform with XSLT; although, keeping the specifics (dollar sign, decimal point, number of digits) may be difficult.

 

The regex solution:

 

<pre>
<?php
$str = '<price>$ 10.99</price>';
echo preg_replace('%((?<=<price>\$\x20)\d+|(?<=\.)\d+(?=</price>))%e', 'str_repeat("-", strlen("$1"))', $str);
?>
</pre>

Here's your previous reply effigy:

%((?<=<price>\$\x20)\d+|(?<=\.)\d+(?=</price>))%e

 

I changed it to this an noticed the substrings went away.

%(?<=<price>\$\x20)\d+|(?<=\.)\d+(?=</price>)%e

 

So am I correct in saying by putting an extra pair of parenthesis around the entire pattern, you can force the full pattern matches into substrings as well?

 

Is there a way to force everything not matched into substrings?

So am I correct in saying by putting an extra pair of parenthesis around the entire pattern, you can force the full pattern matches into substrings as well?

 

Right. If you do not capture anything $1 is empty when it is passed to str_repeat.

 

Is there a way to force everything not matched into substrings?

 

Only matches are stored in the array; you can match the undesirables just to get them in the array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.