Using preg_replace to modularize code

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
bb
Forum Newbie
Posts: 8
Joined: Fri Jun 03, 2005 10:03 pm

Using preg_replace to modularize code

Post by bb »

I'm trying to clean up my code a bit more by taking almost all of the HTML out of it. I'm using a separate file that stores the HTML I need, and using preg_replace to fill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

<tr>
	<td colspan="5">#CATEGORY#</td>
</tr&gt;
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is replacing "CATEGORY" but not the hash's (#)

I'm new to regex's so I'm pretty sure I'm doing something wrongCATEGORY#</td>
&lt;/tr&gt;


the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is replacing "CATEGORY" but not the hash's (#)

I'm new to regex's so I'm pretty sure I'm doing something wrongfill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

<tr>
	<td colspan="5">#CATEGORY#&lt;/td&gt;
&lt;/tr&gt;
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is replacing "CATEGORY" but not the hash's (#)

I'm new to regex's so I'm pretamp;quote;5">#CATEGORY#</td>
</tr>


the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is rep I'm using a separate file that stores the HTML I need, and using preg_replace to fill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

<tr>
	&lt;td colspan=&quote;5&quote;&gt;#CATEGORY#&lt;/td&gt;
&lt;/tr&gt;
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file(&quote;header.html&quote;);
$header = implode(&quote;&quote;, $header_array); 

//Information from the database
$category = $row&#1111;'can up my code a bit more by taking almost all of the HTML out of it. I'm using a separate file that stores the HTML I need, and using preg_replace to fill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

<tr>
	<td colspan="5">#CATEGORY#</td>
</tr>
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is replacing "CATEGORY" but not the hash's (#)

I'm new to regex's so I'm pretty sure I'm doing something wrongand using preg_replace to fill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

&lt;tr>
	<td colspan="5">#CATEGORY#</td>
</tr>
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file(&quote;header.html&quote;);
$header = implode(&quote;&quote;, $header_array); 

//Information from the database
$category = $row&#1111;'category'];

//preg_re
my html is simple, it looks like this:

Code: Select all

&lt;tr&gt;
	&lt;td colspan="5&quote;&gt;#CATEGORY#&lt;/td&gt;
&lt;/tr&gt;
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file(&quote;header.html&quote;);
$header = implode(&quote;&quote;, $header_array); 

//Information from the database
$category = $row&#1111;'category'];

//preg_replace function
print preg_replace(&quote;#CATEGORY#&qu need, and using preg_replace to fill in the data.

It's almost working except for one thing

my html is simple, it looks like this:

Code: Select all

&lt;tr&gt;
	&lt;td colspan=&quote;5&quote;&gt;#CATEGORY#&lt;/td&gt;
&lt;/tr&gt;
the PHP is straightforward too:

Code: Select all

//The HTML file
$header_array = file("header.html");
$header = implode("", $header_array); 

//Information from the database
$category = $row['category'];

//preg_replace function
print preg_replace("#CATEGORY#", $category, $header);
the problem is the result is this
#1#
#2#

meaning it is replacing "CATEGORY" but not the hash's (#)

I'm new to regex's so I'm pretty sure I'm doing something wrong
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to regex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

First: You could actually use str_replace() the exact way you are using it here in place of preg_replace() and it will do what you want.
Second: The reason yours is behaving like you describe is that regex have start and end delimiters... the first character in the pattern (# in this case) idenetifies the delimiters to be used, the pattern should then end with this same character (# again here). The delimiters are not read is part of the pattern, they simply tell it where it starts and stops (like using quotes in PHP to indentify strings). Generally /pattern/ is used but you can use your own delimiters ( @pattern@ ) and here's your done #pattern# where CATEGORY is the pattern and the # just delimit it.

Put simply, add some delimiters....

Code: Select all

/#CATEGORY#/
But like I say, if it's just a static string you're replacing, str_replace() fit's the bill better ;)
Post Reply