[clug-progsig] PHP and regex replace?

Shawn sgrover at open2space.com
Mon Aug 22 21:22:41 PDT 2005


I have the following PHP code in place:

$src = preg_replace("/$curkey/", "$tmp", $src);

And am finding that *sometimes* the $curkey value gets replaced, other times 
it doesn't...  Any tips?  Taking out the slashes produces other blatent 
errors, so I don't think that's it... 

For more details, I'm writing a system to scan for "macros" in a text file, 
then replace it with some other text.  So, the file might contain 

"@@CFFIELDPROPERTIES" (less quotes) someplace in the file, and this should 
look like the following once the replacement is done:

<cfproperty name="job_id" type="numeric">
<cfproperty name="job_type" type="string">
<cfproperty name="ordered_by_id" type="numeric">
<cfproperty name="ordered_by_name" type="string">
<cfproperty name="completed_by_id" type="numeric">
<cfproperty name="Last_Update_Time" type="date">
<cfproperty name="Last_Update_User" type="string">

In some cases, I'm seeing output like this:

@@CFFIELDPROPERTIES<cfproperty name="job_id" type="numeric">
<cfproperty name="job_type" type="string">
<cfproperty name="ordered_by_id" type="numeric">
<cfproperty name="ordered_by_name" type="string">
<cfproperty name="completed_by_id" type="numeric">
<cfproperty name="Last_Update_Time" type="date">
<cfproperty name="Last_Update_User" type="string">

I would prefer to use the regular expresion replace routines, so that my 
"macros" can potentially be more robust than simple text values.  But If I 
can't get this to replace properly, I'll have to resort to simple string 
replacement methods...

Thanks for any suggestions...

Shawn

btw, I'm going to make this code available in the not toooo distant future - 
it reads the structure of a database and generates code to access the 
database.  It's language independent (i.e. other language modules can be 
created easily), and custom macros can easily be added... The above code is a 
sample of the Cold Fusion module I have a pressing need for right now...



More information about the clug-progsig mailing list