Page 1 of 1

[SOLVED] Serious preg_replace headache .. help!!

Posted: Sat Oct 16, 2004 12:31 pm
by tsg
Ok, what I am "trying" to do is replace a string in a text file with a function. Got that part kind of working, but the results are not in the correct order.

Code: Select all

function replaceTest() {
   print "This is a test - YO";
}

Code: Select all

$page['page_text'] = preg_replace('#\[replace]#i', replaceTest(),$page['page_text']);
Now my text I am pulling from the DB is something like this:

Code: Select all

<div>This this the top ...</div>
<div> </div>
<div>їreplace]</div>
<div> </div>
<div>This is the bottom</div>
It replaces and called the function, but it puts the results of the replace above everthing else ... so I get:
This is a test - YO
This this the top ...


This is the bottom
Any ideas on how to get the function to call in the correct place??

Thanks in advance!
Tim

Posted: Sat Oct 16, 2004 12:34 pm
by timvw
my guess is that your function should return and not print the text ;)

Posted: Sat Oct 16, 2004 12:35 pm
by kettle_drum
You might find it easier to use str_replace() for this simple task - [replace] is a fairly easy thing to find in a string and you dont really need use regex for it.

Posted: Sat Oct 16, 2004 12:40 pm
by tsg
DUH!

The return did it!!!!

Thanks!!!! Blood pressure was going up!

Posted: Sat Oct 16, 2004 4:59 pm
by tim
i would take kettles advice tho

str_replace would be faster and wouldnt require envoking the regEx engine