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" 1

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
 
Try something like:
[tt]$modstring = preg_replace("#$invalid#ie", 'str_repeat("*", strlen($1))', $mainstring);[/tt]
The [tt]e[/tt] switch in the regular expression tells the [tt]preg_replace[/tt] function that the string it gets as a second argument is really a string of PHP which should be eval'ed. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top