[solved] Back reference not working

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

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

[solved] Back reference not working

Post by pickle »

Hi all,

I'm trying to safely encapsulate any include() statements that may pop up in webpages in my CMS. What I'm trying to do is use preg_replace to find all occurrences of include("/something/here"); and turn it into:

Code: Select all

<?PHP $func=create_function('','include("/something/here");'); $func(); unset($func); ?>
The code I'm using to do this is:

Code: Select all

$content = preg_replace("/(include\(.*?\))/i", "<?PHP \$func=create_function('', 'include($1);'); \$func(); unset(\$func); ?>", $content);
However, with an input string of:

Code: Select all

include("something here");
include(should not succeed);
this is a blank line;
the resulting string is

Code: Select all

;
;
this is a blank line;
This is very frustrating as this is the only way I know of, to be able to have an aritrary string with PHP code in it, and have that code executed properly.

Any ideas?


Don't worry folks. Turns out I'm a raging idiot who doesn't know how to use str_replace. Thanks anyway.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Re: [solved] Back reference not working

Post by patrikG »

pickle wrote:
Don't worry folks. Turns out I'm a raging idiot who doesn't know how to use str_replace. Thanks anyway.
I won't quote you on that ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [solved] Back reference not working

Post by pickle »

patrikG wrote:I won't quote you on that ;)
And yet you just did :wink:

It was just one of those things that created such rage that I can't use English to describe it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Re: [solved] Back reference not working

Post by patrikG »

pickle wrote:
patrikG wrote:I won't quote you on that ;)
And yet you just did :wink:
Ack, how shameful of me ;)
pickle wrote:It was just one of those things that created such rage that I can't use English to describe it.
I know the feeling only too well :?
Post Reply