After including this new regex in the PHP file in my IDE (Eclipse PDT), I was prompted to save the PHP file as UTF-8 instead of the default cp1252.
After saving and running the PHP file, every time I used a regex in a preg_match() or preg_replace() function call, it generated a generic PHP warning (Warning: preg_match in file.php on line x) and the regex was not processed.
So–two questions:
Is there another symbol that would be good to use as a delimiter that isn’t typically found on a keyboard that I can standardize on and not worry about having to check each and every regex to see if that symbol is actually used somewhere in the expression?
Or, is there a I way I can use the copyright symbol as the standard delimiter when the file format is UTF-8?
I suppose you could try one of the non-printable characters. Here’s how Notepad++ renders them. Those are the “start of heading” and “start of text” control characters.
For years now I have used the tilde (~) since it is rarely used in a regex (possibly never in the ones I have worked on). That has worked very well for me and there should be no issues with character encoding like you’ve stumbled upon here.
If you’re using unknown values in the regex, you should use preg_quote(). Or just use it all the time if you really don’t want to think about it. (not recommended)
Trying to find a non-printable or some other odd character seems to be inviting trouble some day.
Absolutely agree. I don’t actually use them myself, but if even the copyright symbol isn’t sufficient, then it seems like we need to be drastic and/or clever.
I used to use the ~ myself, then later switched to # for no other reason than it seemed to be the more conventional choice.
But it isn’t a problem at all. If you have the delimiter character in your regex, you escape it with a backslash. If you’re working with an unknown regex (in a variable), you escape it with preg_quote(). Using some character not on the keyboard is the problem, that’s why you started this thread.
I can understand the desire to have something you will never need to escape, but never can always happen. Any character could show up in a regex - even the copyright.