Hi there!
How do I pick out angle brackets (<>) with regex? It's for preg_replace.
Thanks.
Pick out angle brackets
Moderator: General Moderators
Re: Pick out angle brackets
Code: Select all
\<- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Pick out angle brackets
'<' an '>' are no meta characters, so there's no need to escape them:jackpf wrote:LolCode: Select all
\<
Code: Select all
$s = 'abc < def';
echo preg_replace('/</', '', $s);Re: Pick out angle brackets
Ahh right, I wasn't sure. I normally escape all non-alphanumeric characters in regex just to be sure. Better safe than sorryprometheuzz wrote:'<' an '>' are no meta characters, so there's no need to escape them:
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Pick out angle brackets
I partly agree with you: it is indeed good to be cautious, but overly escaping characters that need not be escaped will make your regex (even) more complicated to read.jackpf wrote:Ahh right, I wasn't sure. I normally escape all non-alphanumeric characters in regex just to be sure. Better safe than sorryprometheuzz wrote:'<' an '>' are no meta characters, so there's no need to escape them:
Re: Pick out angle brackets
Very true....
But at least, when you're facing problems like this, it won't do any harm to escape all non-alphanumeric characters to see what your problem is...
But at least, when you're facing problems like this, it won't do any harm to escape all non-alphanumeric characters to see what your problem is...