Page 1 of 1

Beginner with ereg_replace

Posted: Mon Jul 17, 2006 4:07 am
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:

Posted: Mon Jul 17, 2006 7:12 am
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.