Beginner with ereg_replace

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
khainodd
Forum Newbie
Posts: 5
Joined: Mon Apr 24, 2006 6:32 am

Beginner with ereg_replace

Post by khainodd »

Hello all

I have a problem with ereg_replace. Well, more like with regular expressions. I'm new to the syntax of regular expressions in php, so I'm having a few problems with them.

I want to replace all text inside # symbols to surround it with the access to an array, like this:

#hello# would be replaced with $array["hello"]

#hello
all# would not be replaced because it contains a \n

the command I created is ereg_replace("\#([^\n])\#", '$array["\\1"]', $stringToReplace)

am I very far away from the solution? :wink:

Thank you all, and sorry if my english is not very good, but I'm from spain :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I wouldn't use ereg_* here, but preg_*.. specifically, preg_replace_callback(). Due to what you are wanting to accomplish, using that function is safer and you'll keep compatibility with future versions of php.
Post Reply