cags Posted October 22, 2009 Share Posted October 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/178593-solved-curly-bracket-delimeters/ Share on other sites More sharing options...
salathe Posted October 22, 2009 Share Posted October 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178593-solved-curly-bracket-delimeters/#findComment-941891 Share on other sites More sharing options...
cags Posted October 22, 2009 Author Share Posted October 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178593-solved-curly-bracket-delimeters/#findComment-941915 Share on other sites More sharing options...
nrg_alpha Posted October 22, 2009 Share Posted October 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178593-solved-curly-bracket-delimeters/#findComment-942178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.