Jump to content

Recommended Posts

I'd always believed that the starting and ending delimeters in preg_ functions had to be the same character, but recently discovered that you can use the curly brackets.

 

Example:

if(preg_match('{[0-9]}', $input)) {

 

I was just wondering if there was any disadvantage/advantage of using this method?

Link to comment
https://forums.phpfreaks.com/topic/178593-solved-curly-bracket-delimeters/
Share on other sites

I'm not sure there is any advantage (unless you can include confusing yourself or whoever else looks at the pattern in the future as an advantage). You can use a pair curly braces or a pair of (), [], or <>.  Aside from being just-another-thing-to-know, and the fact that you can draw amusing pattern-faces (e.g. <(o.o)>) I don't see any benefit.

Thanks salathe, I think the amusing faces could be a big advantage... but I'm not sure how often such patterns would come in handy. :)

 

Oh btw, with regards to the PHP manual that we discussed briefly in the other thread. Whilst I had trouble wrapping my head around the wording used, I'm not sure I could come up with anything better.

Personally, I wouldn't recommend using matching opening / closing punctuation as delimiters.. it's a good setup for confusion in the event those characters appear within the pattern if one isn't careful.. on a side note, since we're discussing odd ball delimiters, I found out a while back that if you use opening / closing matching punctuation as delimiters, you can use those same characters within the pattern without escaping them on the condition that there is matching opening / closing punctuation within the pattern.

 

Examples:

$txt = 'Gibberish <_is all this is._> Some more <_odd ball_> gibberish!';
$txt = preg_replace('<<_[^_]+_>>', '<_XX_>', $txt); // in this particular case, no need to escape inner < or > characters
echo $txt;

 

$txt = 'This is a {test 23468} with numbers!';
preg_match('{{test d{5,9}}}', $txt, $match); // note through the barrage of nested { and } characters, none require escaping.. and the interval {min,max} still functions
echo $match[0];

Granted, in the event inner pattern characters are missing either of their opening or closing counterparts, there's trouble (depending on whether it is the opening or closing character that's missing will determine the warning). A missing closing character within the pattern will lead to a 'No end matching delimiter 'x' found' warning (such as in a pattern like <<_[^_]+_>), or an 'Unknown modifier 'x' warning (such as in a pattern like <_[^_]+_>>). This all assumes of course that there is no inner 'delimiter' characters that are escaped.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.