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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.