Jump to content

[SOLVED] preg_match_all simple question...


x1nick

Recommended Posts

This should be really simple, but can't work it out.

 

I have multiple values eg {username} in a string.

 

Inside the { } brackets, valid charachters are a-z (upper and lower) 0-9 and _

 

preg_match_all('/{([^}]*)}/',$var,$varmatch);		

 

Is my current code, but need to limit this to the above characters only.

What expression should I use?

Link to comment
https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/
Share on other sites

I haven't tested it but how about...

 

'~{[A-Za-z0-9_]*?}~'

 

This may also work, but unfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen.

 

'~{[\w\d_]*?}~'

Iunfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen.

 

'~{[\w\d_]*?}~'

 

Without going into locale issues (which have direct ramifications on what stuff like \w includes), for all intents and purposes, \w = [a-zA-Z0-9_] So in your snippet there, you have added \d and _ for nothing (now that you know what constitutes as \w (more or less / basically).

 

Just to clear up does the * return the content's between the braces?

 

No, the * is known as a 'zero or more times' quantifier (a quantifier, well, quantifies something).

So if you have a pattern like /a*b/, end results could include:

b

ab

aab

aaaaab

aaaaaaaaaaab etc...

 

You can read up on regex in the following links to help you better understand it:

 

http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

http://www.regular-expressions.info/

http://weblogtoolscollection.com/regex/regex.php

http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.