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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Question 3

Status
Not open for further replies.

ZimZangZoom

Programmer
Dec 17, 2004
26
Hello,

I am currently making a program that will allow people to use custom tags that i have specified in the script.
For example: #NAME# is replaced with $name

Currently i have been using code such as.
$body =~s/#NAME#/$name/g;

But what if i wanted to replace #NAME# with a sub routine called &name;

How can i do such a process ?

Drew
 
Well maybe i figured it out.. But i dont know if it is thebest way,., this works.. but is there anything better ?


if ($body =~ /#NAME#/i) {
&name;
}
 
You can use the /e flag in your regexp to have the replacement treated as code, rather than merely as a quoted string:
Code:
$body =~s/#NAME#/name()/ge;
Now, #NAME# will be replaced by the whatever name() returns.
 
ish, this pointed out even after beers, is brilliance ...
mait thú, a mhic

--paul

cigless ...
 
a word of caution on substitution with execution
I'm just after coming across a vulnerability with phpBB 2.0, where a value that's submitted is substituted in such a way.

Causing arbitrary code to be run on the server.

because you're calling a specified routine, it won't be an issue here, but if you're considering running such a regex on user submitted values.


Don't be crying when there's bainne ar an úrlar (For Mike: spilt milk)


Happy Holidays
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top