Forum Moderators: coopster

Message Too Old, No Replies

REGEX substituion on PHP source BEFORE PHP processes it?

         

u2ill

5:59 pm on Aug 6, 2008 (gmt 0)

10+ Year Member



Hello everybody! I'm new here so take it easy on me!

Does anyone know if a REGEX substitution on PHP source code can be made by PHP before or AFTER it processes the source ?

For example, if the PHP source contains:

<a href="mailto:email@domain.com">email@domain.com</a>

Is there an option in PHP to on-the-fly do a regex substitution to search and replace the above code with something else?

Yes, I know I could just search/replace the actual coded files, but the goal is to not touch the actual source files, just do on-the-fly replacements of certain strings BEFORE or just AFTER PHP processes it.

Does anyone know if this can be done, and if so, how?

Thanks in advance!

eelixduppy

5:00 am on Aug 7, 2008 (gmt 0)



No, it cannot be done how you want it done as far as I know. What can be done, though, is you store the string in a variable and change the contents of that upon certain conditions.


if(condition 1) {
$string = '<a href="mailto:email@domain.com">email@domain.com</a>';
} else {
$string = 'something else...';
}

Make sense?

dcelasun

7:30 am on Aug 11, 2008 (gmt 0)

10+ Year Member



You can populate all the output (the page in your example) in a string and then use preg_replace() or str_replace() like:

// Get all your output into $output by using $output .= some thing
// Then:
$output = str_replace([whatever you want to do],$output);
echo $output; // $output contains everything between <html> and </html>

Makes sense?

william white

4:11 am on Aug 14, 2008 (gmt 0)

10+ Year Member



Actually, it can be done using the newest version of Apache and mod_substitute. There is a blog out there somewhere that explains how to do it.

coopster

9:05 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



u2ill dropped an example in Apache:

[webmasterworld.com...]

mod_substitute [httpd.apache.org] is available in Apache 2.2.7 and later. However, it is also an extension [httpd.apache.org], not base. So unless you have control over your server or a lot of pull with your hosting provider, the Apache mod_substitute method may not be an option for you.

As mentioned, other options include using output buffering to do your own substitution using find/replace or regular expression replacement.