Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specific problem with PERL Regular Expressions -"dynamic substitution"

Status
Not open for further replies.

Geneith

Programmer
Mar 11, 2003
4
CA
I'm fairly new to regular expressions in general and one of my learning projects is to create a filter that will substitute select words, or combination of characters, with the appropriate number of *'s - corresponding to the number of characters that match the substring to be replaced.

For example, let's say I add the word 'blah' to the filter. I would like it to be replaced with '****', and 'myveryownwordwithnospaces' with '************************'.

I'm quite comfortable now with pattern syntax and all, but I'm at a loss for trying to figure out how to add 'dynamically substituted substring' code.

Consider the following example:

$modstring = preg_replace("#$invalid#i", '*moderated*', $mainstring);

That is what I would call a static substitution. No matter how long the "$invalid" word, it will always be replaced with *moderated*. I have tried for days to figure out how I might substitute each character in a matched substring with a * (thereby effectively adding the appropriate number of *'s for a matched substring of a given length).

Thanks in advance for any help you can provide :)

Geneith

--- One thing to note: I'm using PERL Regular Expressions, but with PHP in case you find that relevant.
 
'Doh! After having searched (unsuccessfully) for a PHP forum, I posted this. Now I have found where they hid it.

I realize that some (or even all) of the solution could be affected by which language it is in (despite the fact that the regular expressions are both PERL), so I will post it over there too. Sorry about the mixup :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top