[SOLVED] Serious preg_replace headache .. help!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

[SOLVED] Serious preg_replace headache .. help!!

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

my guess is that your function should return and not print the text ;)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

DUH!

The return did it!!!!

Thanks!!!! Blood pressure was going up!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i would take kettles advice tho

str_replace would be faster and wouldnt require envoking the regEx engine
Post Reply