Jul 18, 2001, 1:20 AM
Post #5 of 11
yes, there's a problem with your regex. The vertical bar | is the regex alternation operator. So you alternate nothing special and nothing special, and nothing special, of course, matches always. The regex engine advances one character and again finds a match for nothing special, and so on, so $msg will be filled with 0's afterwards, which of course then translate back to vertical bars. So, to have this fixed, escape the vertical bar:
But all in all, you're going to have a problem with your solution: What if someone uses @'s or 0's in his posts all the time? You're going to store them as they are, but translate them back to +!+'s and |'s.
One thing that comes to my mind right now (I guess there's plenty of better solutions) is to escape some of the special characters in your database. Test this piece of code:
The first part will replace all occurrences of !, = or \ with \!, \= or \\. So all possible +!+ sequences will be converted to +\!+ and you don't have problems with these. It's not so easy with the |'s however. But since we have escaped all ='s, we can safely use = as a replacement for |. This is done with the tr/// line. The text now looks like this:
$msg = <<'ENDMSG';
Hello Pro_4 | whoever!
Here's my +!+ message +!+ for you!
== \Marcus\ ==
$msg =~ s/([!=\\])/\\$1/g;
$msg =~ tr/|/=/;
$msg =~ s/\\(.)|=/$1||'|'/ge;
The next regex is for reversing these changes so you get the original message back. It will look for an escaped character OR an equal sign =. If it finds an escaped character, it removes the backslash, if it finds the equal sign, it is replaced by a vertical bar.
Hello Pro_4 = whoever\!
Here's my +\!+ message +\!+ for you\!
\=\= \\Marcus\\ \=\=
I'm more than sure there are better solutions, but this one was the first that came to my mind.
Hope this helps anyway.