Re: [PHP] Regular Expression help
Arpad Ray wrote: Note that $ allows a trailing newline, but \z doesn't. I had to test that before believing you: php -r 'var_dump(preg_match(#^[a-z]+\$#,abc),preg_match(#^[a-z]+\$#,abc\n),preg_match(#^[a-z]+\z#,abc\n));' you are right, that could consitute a nice big gotcha in some situations, although I have the habit of running trim on everything bit of data that has to be validated/santized (prior to any more specific checks/cleansing) so I have been protecting my self unwittingly: php -r 'var_dump(abc\n, trim(abc\n));' none the less I think I'll be using \z more often now that I have been properly introduced to it (including answering any regexp questions around here!) thanks Arpad :-) Arpad Stut wrote: Chris Boget wrote: ?php echo 'Is String: [' . ( is_string( 'a1b2c3' ) preg_match( '/[A-Za-z]+/', 'a1b2c3' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( 'a1b2c3' ) preg_match( '/[0-9]+/', 'a1b2c3' )) . ']br'; echo 'Is String: [' . ( is_string( 'abcdef' ) preg_match( '/[A-Za-z]+/', 'abcdef' )) . ']br'; echo 'Is Numeric: [' . ( is_numeric( '123456' ) preg_match( '/[0-9]+/', '123456' )) . ']br'; ? Why is the first Is String check returning true/showing 1? preg_match should fail because 'a1b2c3' contains numbers and, as such, doesn't match the pattern... It does match the pattern. The expression says 1 or more A-Za-z in sequence. If you want to check against the whole string you need to add the start and end markers... preg_match( '/^[A-Za-z]+$/', 'a1b2c3' )) Look at the manual page for preg_match and read up on the third parameter. Use it to see what is matching the expression. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression to find from start of string to first space
PHP, Shouldn't this regular expression select everything from the start of the string to the first space character: $firstWord = preg_match('#^*(.*) #iU', $word); It doesn't, so clearly I'm wrong, but here's why I thought it would: The enclosing has marks, #, I *think* just encloses the expression. I was told to use them before, but I can't find them here: http://jp2.php.net/manual/en/reference.pcre.pattern.syntax.php The caret, ^, says to start at the beginning of the line. The first asterix, * after the caret says to use any starting character. The space just before the second # is the closing character of my search. The (.*) in the middle says to take anything in between the beginning of the line and the space. iU says, be case insensitive, and don't be greedy. So, it should start at the beginning of the line and get everything up to the first space. But it doesn't work. Where did I go wrong? -- Dave M G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression to find from start of string to first space
Dave M G wrote: PHP, Shouldn't this regular expression select everything from the start of the string to the first space character: $firstWord = preg_match('#^*(.*) #iU', $word); It doesn't, so clearly I'm wrong, but here's why I thought it would: The enclosing has marks, #, I *think* just encloses the expression. I was told to use them before, but I can't find them here: http://jp2.php.net/manual/en/reference.pcre.pattern.syntax.php The caret, ^, says to start at the beginning of the line. The first asterix, * after the caret says to use any starting character. Here's where you go wrong. The * means match the previous character 0 or more times. In this case I'm actually not sure what it would do. Given that you grab everything with the (.*) the first * is not needed at all. David -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression to find from start of string to first space
On 08/08/06, Dave M G [EMAIL PROTECTED] wrote: PHP, Shouldn't this regular expression select everything from the start of the string to the first space character: $firstWord = preg_match('#^*(.*) #iU', $word); It doesn't, so clearly I'm wrong, but here's why I thought it would: . stands for any single character, not *. Also, # is the delimiter, but you can use a variety of deliimters. The following should work: ^[\w\d]+\s{1} ^ start of line [\w\d]+ range of any digits or letters, one or more (\s{1}) exactly one whitespace character hope this helps! -- http://www.web-buddha.co.uk http://www.projectkarma.co.uk
Re: [PHP] Regular expression to find from start of string to first space
On Tue, August 8, 2006 4:21 am, Dave M G wrote: Shouldn't this regular expression select everything from the start of the string to the first space character: $firstWord = preg_match('#^*(.*) #iU', $word); It doesn't, so clearly I'm wrong, but here's why I thought it would: The enclosing has marks, #, I *think* just encloses the expression. I was told to use them before, but I can't find them here: http://jp2.php.net/manual/en/reference.pcre.pattern.syntax.php The # can be any character you want, that's convenient. Convenient generally means not likely to be needed within the pattern The #, or whatever you choose, marks beginning and end of the pattern. I believe that there are also some special ones like and that you can use. The caret, ^, says to start at the beginning of the line. Yes. The first asterix, * after the caret says to use any starting character. No. * means 0 or more of the preceding thingie * by itself, with nothing preceding it... I don't even know WHAT that means. Maybe it's just a * sometimes a star is just a star :-) Or maybe it applies to the beginning of string anchor, so it would match 0 or more newlines if you were using 's' at the end... But the whole point of ^ is to require a start, and the whole point of * is to not require anything at all, so that would be a oxymoron. The space just before the second # is the closing character of my search. No. The # is the closing of your pattern, because you CHOSE # as the beginning of your pattern. These are all the same: #^(.*)\s# |^(.*)\s| /^(.*)\s/ Z^(.*)\sZ Z is probably not such a good idea as you may need a Z in your pattern. It may even be illegal and you can only use non-alphanumeric for the delimiter, actually. The (.*) in the middle says to take anything in between the beginning of the line and the space. iU says, be case insensitive, and don't be greedy. Yes. Without that, the .* would match spaces as well as non-spaces, and only the LAST whitespace would count for the \s bit. So, it should start at the beginning of the line and get everything up to the first space. But it doesn't work. I generally find .* to be problematic, and do more like: #^([^\s])*#sU This way, I'm saying to anchor at the beginning, and then look for NON-whitespace, and use * to get as many as possible. The ^ *inside* the [] means not -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression to find from start of string to first space [SOLVED]
Richard, Adam, Barry, Dave, David, Thank you all for your helpful advice regarding expressions. I was able to combine all your advice, and made some additional discoveries along the way. The winning expression is: #^(.*)\s#iU First, I discovered that sometimes the source text had an unexpected space character at the very beginning. So I realized that I needed to do trim() to ensure proper functionality of the expression. Next, it seems it's better to use \s than an actual space, . It might be the case that both are okay, but I've had success with \s, so I decided to stop experimenting. I tried, as Adam suggested, to use substr() instead of a regular expression. The syntax he provided may work as far as finding the right text to extract. But I can't be sure because it seems to have problems with the character encoding. The word being extracted is in UTF-8 encoded Japanese. The regular expression seems to input and output it fine. But I couldn't find a way of using the substr() function without the text coming out as ASCII gibberish. So I'm calling this one solved for me, as I have working code. Although if anyone believes that it could be more efficient or something, of course I'm all ears. Thank you for all your time and advice. -- Dave M G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression to extract from the middle of a string
Hey folks I don't want to just get you to do the work, but I have so far tried in vain to achieve something... I have a string similar to the following; cn=emailadmin,ou=services,dc=domain,dc=net I want to extract whatever falls between the 'cn=' and the following comma - in this case 'emailadmin'. Question(s); is this possible via a regular expression? does php have a better way of doing this? Some pointers would be greatly appreciated. Once I have working, I will be creating a function which will cater for this and will post to this list if anyone is interested? Cheers Steve -- Steve Turnbull Digital Content Developer YHGfL Foundation e [EMAIL PROTECTED] t 01724 275030 The YHGfL Foundation Disclaimer can be found at: http://www.yhgfl.net/foundation-services/yhgfl-email-disclaimer/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression to extract from the middle of a string
On 14/07/06, Steve Turnbull [EMAIL PROTECTED] wrote: Hey folks I don't want to just get you to do the work, but I have so far tried in vain to achieve something... I have a string similar to the following; cn=emailadmin,ou=services,dc=domain,dc=net I want to extract whatever falls between the 'cn=' and the following comma - in this case 'emailadmin'. Question(s); is this possible via a regular expression? does php have a better way of doing this? Some pointers would be greatly appreciated. Once I have working, I will be creating a function which will cater for this and will post to this list if anyone is interested? Cheers Steve I think you will have to use minimal matching to ensure you only grab the first sequence. the following pattern should do the trick: '/cn=(\w+,?) -- http://www.web-buddha.co.uk http://www.projectkarma.co.uk
Re: [PHP] regular expression to extract from the middle of a string
I believe someone gave the regex code for it already, but if you wanted to do it the clumsy way (for those of us who are regex challenged still) here's an alternative: $str = cn=emailadmin,ou=services,dc=domain,dc=net; $argsarray = explode(,, $str); foreach ($argsarray as $argstr) { list($arg, $val) = explode(=, $argstr); $newarr[$arg] = $val; } echo $newarr['cn']; Then you have access to all the parameters being given. I'm pretty sure there's a more elegant way to do this even the clumsy way I did it, but just giving a basic example of how else you might be able to get that information. -TG = = = Original message = = = Hey folks I don't want to just get you to do the work, but I have so far tried in vain to achieve something... I have a string similar to the following; cn=emailadmin,ou=services,dc=domain,dc=net I want to extract whatever falls between the 'cn=' and the following comma - in this case 'emailadmin'. Question(s); is this possible via a regular expression? does php have a better way of doing this? Some pointers would be greatly appreciated. Once I have working, I will be creating a function which will cater for this and will post to this list if anyone is interested? Cheers Steve -- Steve Turnbull ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression to extract from the middle of a string
On 7/14/06, Steve Turnbull [EMAIL PROTECTED] wrote: I have a string similar to the following; cn=emailadmin,ou=services,dc=domain,dc=net I want to extract whatever falls between the 'cn=' and the following comma - in this case 'emailadmin'. $pattern= /[^=]+=([^,]+)/; preg_match($pattern, $string, $matches); print_r($matches); Voila! (Untested for now, I'm pretty drunk so sorry if it ain't workin out like you want to) -- Kim Christensen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression to extract from the middle of a string
Ill probably get attacked viciously for this with pitchforks and machetes, but I get sick and tired of trying to figure out regular expressions a lot of times, so I use the following functions... getSingleMatch(), getMultiMatch(), getSingleMatchBackwards() function getSingleMatch($start,$end,$content) { // finds the first match giving a beginning and a part of string that you want to grab // eg: to get the title from an html document, you would just use the command getSingleMatch('title','/title',$html); $exp = explode($start,$content); $exp2 = explode($end,$exp[1]); return $exp2[0]; } function getMultiMatch($start,$end,$content) { // finds all the non-embeded matches based on a beginning and ending string // eg: to get all the h1 tags in an html document, you would use getMultiMatch('h1','/h1',$html); $exp = explode($start,$content); foreach($exp as $pi) { if(stristr($pi,$end)) { $ex2 = explode($end,$pi); $matches[] = $ex2[0]; } } return $matches; } function getSingleMatchBackwards($start,$end,$content) { // the same as getSingleMatch except it goes backwards to forwards. This helps in cases where the // most distinct delimiter is at the end of your target rather than the beginning. $exp = explode($end,$content); $exp2 = explode($start,$exp[0]); return $exp2[count($exp2)-1]; } On 7/14/06, Kim Christensen [EMAIL PROTECTED] wrote: On 7/14/06, Steve Turnbull [EMAIL PROTECTED] wrote: I have a string similar to the following; cn=emailadmin,ou=services,dc=domain,dc=net I want to extract whatever falls between the 'cn=' and the following comma - in this case 'emailadmin'. $pattern= /[^=]+=([^,]+)/; preg_match($pattern, $string, $matches); print_r($matches); Voila! (Untested for now, I'm pretty drunk so sorry if it ain't workin out like you want to) -- Kim Christensen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
On 2/14/06, Patrick [EMAIL PROTECTED] wrote: Hi, I am trying to validate a password, but havent figured out the pattern for it yet. The password must contain atleast 6 characters a-zA-Z0-9_ must start with a a-zA-Z and must have atleast one of the following characters !#%$£ As Curt said, it's probably better to do this programatically - if only so you can work out what it's doing six months down the road. However if you want to know to do it as a regexp: start with the letters a-z. ^[a-z] At least 5 legal characters following that (use a lookahead assertion to check): (?=[!#%$£\w]{5}) Zero or more non-punctuation characters, followed by a a punctuation character, followed zero or more of any valid character. \w*[!#%$£][!#%$£\w]* And then anchor the end of the string with a $. Put all that together with a bit of case insensitivity and you get. $regexp = '/^[a-z](?=[!#%$£\w]{5})\w*[!#%$£][!#%$£\w]*$/i'; $status = preg_match($regexp, $password) ? 'PASS' : 'FAIL'; -robin
RE: [PHP] Regular expression
Check out some Regular Expression code examples To learn more : http://www.weberdev.com/AdvancedSearch.php?searchtype=categorycategory=Rege xps Sincerely berber Visit the Weber Sites Today, To see where PHP might take you tomorrow. Uptime Monitor : http://uptime.weberdev.com SEO Data Monitor http://seo.weberdev.com -Original Message- From: Patrick [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 14, 2006 11:53 PM To: php-general@lists.php.net Subject: [PHP] Regular expression Hi, I am trying to validate a password, but havent figured out the pattern for it yet. The password must contain atleast 6 characters a-zA-Z0-9_ must start with a a-zA-Z and must have atleast one of the following characters !#%$£ correct passwords would be: a#aAb08 Plkpod! t09_#8T U_p#q#Pq i was trying something like this, but it dosent seem to work: ^[a-zA-Z]{1}[!#%$]+[a-zA-Z0-9_]+ Patrick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
Weber Sites LTD wrote: Check out some Regular Expression code examples To learn more : http://www.weberdev.com/AdvancedSearch.php?searchtype=categorycategory=Rege xps Sincerely berber ZONG! - No results were found. * Run this seach again but include PHP Functions in the results. * Did you try our AdvancedSearch? * How about checking the forums? * Maybe our Links Index? * Go Back and try again? - ? -- Smileys rule (cX.x)C --o(^_^o) Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular expression
If you make sure the link does not break you will get results :) http://www.weberdev.com/AdvancedSearch.php?searchtype=categorycategory=Rege xps -Original Message- From: Barry [mailto:[EMAIL PROTECTED] Sent: Wednesday, February 15, 2006 5:20 PM To: php-general@lists.php.net Subject: Re: [PHP] Regular expression Weber Sites LTD wrote: Check out some Regular Expression code examples To learn more : http://www.weberdev.com/AdvancedSearch.php?searchtype=categorycategor y=Rege xps Sincerely berber ZONG! - No results were found. * Run this seach again but include PHP Functions in the results. * Did you try our AdvancedSearch? * How about checking the forums? * Maybe our Links Index? * Go Back and try again? - ? -- Smileys rule (cX.x)C --o(^_^o) Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression
Hi, I am trying to validate a password, but havent figured out the pattern for it yet. The password must contain atleast 6 characters a-zA-Z0-9_ must start with a a-zA-Z and must have atleast one of the following characters !#%$£ correct passwords would be: a#aAb08 Plkpod! t09_#8T U_p#q#Pq i was trying something like this, but it dosent seem to work: ^[a-zA-Z]{1}[!#%$]+[a-zA-Z0-9_]+ Patrick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
Patrick, http://regexlib.com/ is a really good site to find examples of regular expressions. http://regexlib.com/Search.aspx?k=password should get you a bunch of results. Also anything else besides letters and numbers is considered bad password form. D On Feb 14, 2006, at 3:53 PM, Patrick wrote: Hi, I am trying to validate a password, but havent figured out the pattern for it yet. The password must contain atleast 6 characters a-zA-Z0-9_ must start with a a-zA-Z and must have atleast one of the following characters !#%$£ correct passwords would be: a#aAb08 Plkpod! t09_#8T U_p#q#Pq i was trying something like this, but it dosent seem to work: ^[a-zA-Z]{1}[!#%$]+[a-zA-Z0-9_]+ Patrick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
On Tue, Feb 14, 2006 at 10:53:17PM +0100, Patrick wrote: Hi, I am trying to validate a password, but havent figured out the pattern for it yet. The password must contain atleast 6 characters a-zA-Z0-9_ must start with a a-zA-Z and must have atleast one of the following characters !#%$� correct passwords would be: a#aAb08 Plkpod! t09_#8T U_p#q#Pq i was trying something like this, but it dosent seem to work: ^[a-zA-Z]{1}[!#%$]+[a-zA-Z0-9_]+ Trying to write a regex with that ruleset i think would be a rather complicated beast, besides you might want to give some feedback based on why it is not a valid password. Basically: ok = false if ! /^[a-z]{1}[a-z!#%$_]{5,}$/i your password doesnt follow the format required else if strlen(pass) 32 # assuming 32 char limit password to long else if pass doesnt have !#%$� i'm forcing you to use a char you maynot want in your password else ok = true Curt. -- cat .signature: No such file or directory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
$last_comma = strrpos($string, ,); $string = substr($string, 0, $last_comma) . ' and ' . substr($string, $last_comma); I probably have a one-off error in there somewhere, or swapped the order of args in strrpos, but you get the idea. http://php.net/strrpos On Mon, January 30, 2006 8:09 am, Barry wrote: Simple reg help please i want to match the last , in a,b,c,d and replace it with and i tried ereg_replace(,([a-zA-z])*$, and ,$string); but i forgot how to add the d which is also matched now back to the and Can you give any good reg_exp sites where to learn it? Its long ago since i used reg exp and i lost the hang of it... :( btw. any sites that have reg_exp that works witht PHP would be fine. i know http://www.regular-expressions.info/tutorial.html But that examples dont work with preg_match and ereg. Thanks ^_^ -- Smileys rule (cX.x)C --o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular expression
Ok I may actually have an answer here... ? $str = 'hubble, bubble, toil, trouble'; function andificate($str) { return preg_replace('/([\s]?,[\s]?)(\w+)$/', ' and \\2', $str); } echo andificate($str); ? Gives: hubble, bubble, toil and trouble Accepts spaces before and/or after commas or neither... preserves spaces Hope this one works (and that I've read the post right :-/) Dan -- -Original Message- From: Richard Lynch [mailto:[EMAIL PROTECTED] Sent: 03 February 2006 23:34 To: Barry Cc: php-general@lists.php.net Subject: Re: [PHP] Regular expression $last_comma = strrpos($string, ,); $string = substr($string, 0, $last_comma) . ' and ' . substr($string, $last_comma); I probably have a one-off error in there somewhere, or swapped the order of args in strrpos, but you get the idea. http://php.net/strrpos On Mon, January 30, 2006 8:09 am, Barry wrote: Simple reg help please i want to match the last , in a,b,c,d and replace it with and i tried ereg_replace(,([a-zA-z])*$, and ,$string); but i forgot how to add the d which is also matched now back to the and Can you give any good reg_exp sites where to learn it? Its long ago since i used reg exp and i lost the hang of it... :( btw. any sites that have reg_exp that works witht PHP would be fine. i know http://www.regular-expressions.info/tutorial.html But that examples dont work with preg_match and ereg. Thanks ^_^ -- Smileys rule (cX.x)C --o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php __ NOD32 1.1393 (20060203) Information __ This message was checked by NOD32 antivirus system. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression
Simple reg help please i want to match the last , in a,b,c,d and replace it with and i tried ereg_replace(,([a-zA-z])*$, and ,$string); but i forgot how to add the d which is also matched now back to the and Can you give any good reg_exp sites where to learn it? Its long ago since i used reg exp and i lost the hang of it... :( btw. any sites that have reg_exp that works witht PHP would be fine. i know http://www.regular-expressions.info/tutorial.html But that examples dont work with preg_match and ereg. Thanks ^_^ -- Smileys rule (cX.x)C --o(^_^o) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
Barry wrote: Simple reg help please i want to match the last , in a,b,c,d and replace it with and Without using a regexp, you could do: code $string = 'a,b,c,d'; $letters = explode(',', $string); $last_letter = array_pop($letters); $final_string = implode(',', $letters) . ' and ' . $last_letter; /code Not very efficient - but should work... Silvio -- tradeOver | http://www.tradeover.net ...ready to become the King of the World? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression help
I've been beating my head against the wall for a while trying to come up with the RE I need to use. I have a camel case word - let's use JimJoeBobBriggs. I need to come up with a RE that will fine all the upper case characters and insert an underscore prior to those characters with the exception of a possible first character. So using the aforementioned word, the RE would change it to Jim_Joe_Bob_Briggs. Any help would be greatly appreciated! thnx, Chris
Re: [PHP] Regular Expression help
Chris Boget wrote: I've been beating my head against the wall for a while trying to come up with the RE I need to use. I have a camel case word - let's use JimJoeBobBriggs. I need to come up with a RE that will fine all the upper case characters and insert an underscore prior to those characters with the exception of a possible first character. So using the aforementioned word, the RE would change it to Jim_Joe_Bob_Briggs. $result = preg_replace('#(.)([A-Z])#', '\1_\2', 'JimJoeBobBriggs'); -- Richard Heyes http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
On 1/19/06, Chris Boget [EMAIL PROTECTED] wrote: I've been beating my head against the wall for a while trying to come up with the RE I need to use. I have a camel case word - let's use JimJoeBobBriggs. I need to come up with a RE that will fine all the upper case characters and insert an underscore prior to those characters with the exception of a possible first character. So using the aforementioned word, the RE would change it to Jim_Joe_Bob_Briggs. ?php $string = 'JimJoeBobBriggs''; print preg_replace('/([[:alnum:]])([[:upper:]])/', '\1_\2', $string); ?
Re: [PHP] Regular Expression help
Chris Boget wrote: I've been beating my head against the wall for a while trying to come up with the RE I need to use. I have a camel case word - let's use JimJoeBobBriggs. I need to come up with a RE that will fine all the upper case characters and insert an underscore prior to those characters with the exception of a possible first character. So using the aforementioned word, the RE would change it to Jim_Joe_Bob_Briggs. Any help would be greatly appreciated! thnx, Chris Those two had valid ones as well, but I like the negative look-behind assertion: preg_replace('/(?!^)([A-Z])/','_$1','JimJoeBobBriggs'); Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
Chris Boget wrote: I've been beating my head against the wall for a while trying to come up with the RE I need to use. I have a camel case word - let's use JimJoeBobBriggs. I need to come up with a RE that will fine all the upper case characters and insert an underscore prior to those characters with the exception of a possible first character. So using the aforementioned word, the RE would change it to Jim_Joe_Bob_Briggs. Any help would be greatly appreciated! preg_replace ( /(.)([A-Z])/, \1_\2, $string ); -- John C. Nichel IV Programmer/System Admin (ÜberGeek) Dot Com Holdings of Buffalo 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression help
Chris wrote: snip preg_replace('/(?!^)([A-Z])/','_$1','JimJoeBobBriggs'); Ohhhregex-fu black belt. ;) -- John C. Nichel IV Programmer/System Admin (ÜberGeek) Dot Com Holdings of Buffalo 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression for integer range
Murray @ PlanetThoughtful [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. You could simplify the matter by replacing all - and , with, say, 0 and then using the simple \d+ (I think) regexp. Hi babu, As you've pointed out, you have 4 distinct scenarios to deal with, and it may be very difficult, without a great deal of tuning and tweaking, to define a regular expression that can effectively match all 4 scenarios without including false matches as well. One way of dealing with this is to build more specialized and exact regular expressions for each possible scenario and group them together in an if statement. Eg. if (preg_match('/^\d+$/',$subject) || preg_match('/^\d+,$/',$subject) || preg_match('/^\d+,\d+-\d+$/', $subject) || etc etc){ // code for successful match of valid data in $subject } else { // code for invalid data in $subject } Basically, the if/else statement is testing each distinct possible pattern and executing code if any of those distinct possible patterns match. It may not ultimately be the most graceful way of dealing with the situation, but having spent many hours attempting to tweak complex regular expressions looking for the magic combination, I've learned that breaking scenarios down this way can save a lot of development time and frustration. This doesn't mean there isn't a benefit to finding the perfect regular expression for your needs, just that it can often be difficult to guarantee your code won't be plagued by false matches or false exclusions as your expression becomes more and more complex. Hope this helps a little. Much warmth, Murray --- Lost in thought... http://www.planetthoughtful.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression for integer range
On 9/6/05, babu [EMAIL PROTECTED] wrote: Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. $re = '/^(\d+)(,\d+-\d+)?$/'; preg_match($re, $value); This only allows * an integer or * an integer followed by a comma and a range of integers It doesn't allow anything else - even whitespace after the comma. -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] regular expression for integer range
Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. Hi babu, As you've pointed out, you have 4 distinct scenarios to deal with, and it may be very difficult, without a great deal of tuning and tweaking, to define a regular expression that can effectively match all 4 scenarios without including false matches as well. One way of dealing with this is to build more specialized and exact regular expressions for each possible scenario and group them together in an if statement. Eg. if (preg_match('/^\d+$/',$subject) || preg_match('/^\d+,$/',$subject) || preg_match('/^\d+,\d+-\d+$/', $subject) || etc etc){ // code for successful match of valid data in $subject } else { // code for invalid data in $subject } Basically, the if/else statement is testing each distinct possible pattern and executing code if any of those distinct possible patterns match. It may not ultimately be the most graceful way of dealing with the situation, but having spent many hours attempting to tweak complex regular expressions looking for the magic combination, I've learned that breaking scenarios down this way can save a lot of development time and frustration. This doesn't mean there isn't a benefit to finding the perfect regular expression for your needs, just that it can often be difficult to guarantee your code won't be plagued by false matches or false exclusions as your expression becomes more and more complex. Hope this helps a little. Much warmth, Murray --- Lost in thought... http://www.planetthoughtful.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression for integer range
Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. thanks babu. - How much free photo storage do you get? Store your holiday snaps for FREE with Yahoo! Photos. Get Yahoo! Photos
Re: [PHP] regular expression for integer range
On Tue, 6 Sep 2005, babu wrote: Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. ereg(^[-0-9,]+$... would do it, but it would also allow 10,- as a valid expression as well.. so if you want to check for valid ranges you'll want to beef that up a bit... -philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression for integer range
babu wrote: Hi all, I want to write regular expression for checking the string format entered by user. the allowed formats are examples: 10 10, 10,12-10 12-10 that is the valid strings are: 1. only integer 2. an integer, range of integers example 3 and no other characters must be allowed. Crude, but it will work preg_match ( /^[-0-9,]+$/, $value ) It will also match just dashes and/or just comma's with no numbers. http://us2.php.net/manual/en/reference.pcre.pattern.syntax.php -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression for time
HI, how can i write regular expression for time in 24-hour format i:e, HH:MM:SS. using preg_match. thanks babu - How much free photo storage do you get? Store your holiday snaps for FREE with Yahoo! Photos. Get Yahoo! Photos
Re: [PHP] regular expression for time
Hello babu, Monday, August 29, 2005, 6:50:32 AM, you wrote: how can i write regular expression for time in 24-hour format i:e, HH:MM:SS. using preg_match. ?php $xtime=19:59:53; if (preg_match(/([01][0-9]|[2][0-3]):[0-5][0-9]:[0-5][0-9]/, $xtime)) echo Good; else echo Bad; ? --TBUDL/BETA/DEV/TECH Lists Moderator / PGP 0x6C0AB16B __ Geocaching:http://gps.PCWize.com ( ) ( ___)(_ _)( ___) TBUDP Wiki Site: http://www.PCWize.com/thebat/tbudp )(__ )__) _)(_ )__) Roguemoticons Smileys:http://PCWize.com/thebat ()()()(__)PHP Tutorials and snippets:http://www.DevTek.org Save your pennies. The dollars go to the I.R.S. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
On 8/11/05, Leon Vismer [EMAIL PROTECTED] wrote: Hi Robin Many thanks for this, how would one extend this to support the following: $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'mcDonald'); one does not want $str = insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'mc_donald'); $match = '/(?=[a-z])(?![Mm]c|[Mm]ac)([A-Z]+)/e'; Should make exceptions for McDonald, mcDonald, MacDonald and macDonald. With luck you don't have any tables called something like appleMacUsers. unfortunately lookbehind assertions does not support non-fixed length chars so /(?=(?!')[a-z])([A-Z]+)/e will work for 'mDonald' but the following will not work. True, lookbehind assertions must be a fixed length - but unlike in perl, they can have alternates which are different fixed lengths: This is illegal, because the length isn't fixed: (?![Mm]a?c) This is fine, because each alternate is fixed even though they are diffferent lengths: (?![Mm]c|[Mm]ac) -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression question
Hi I would like to convert from one naming convention within a sql statement to another. I have the following, code $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'Vismer'); $match = array( /([a-z]+)(ID)/, /([a-z]+)([A-Z])/ ); $replace = array( \$1_id, \$1_\$2 ); $nstr = preg_replace($match, $replace, $str); echo $nstr .\n; /code the above gets me to insert into user_Comment (user_id, user_Name, user_Surname) values (0, 'Leon', 'Vismer') however I want to get to insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'Vismer') Some help from the regex experts ;-) Many thanks -- Leon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
n Thu, 11 Aug 2005, Leon Vismer wrote: Hi I would like to convert from one naming convention within a sql statement to another. I have the following, code $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'Vismer'); $match = array( /([a-z]+)(ID)/, /([a-z]+)([A-Z])/ ); $replace = array( \$1_id, \$1_\$2 ); $nstr = preg_replace($match, $replace, $str); echo $nstr .\n; /code the above gets me to insert into user_Comment (user_id, user_Name, user_Surname) values (0, 'Leon', 'Vismer') however I want to get to insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'Vismer') Some help from the regex experts ;-) Just a quick note; why dont' you search on user since it's the constant and replace 'user[A-Z]' with 'user_[a-z]' or in the case of userID 'user[A-Z]{2}' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
Hi Just a quick note; why dont' you search on user since it's the constant and replace 'user[A-Z]' with 'user_[a-z]' or in the case of userID 'user[A-Z]{2}' This is part of my problem user will not always be constant, I basically want to be able to change between two naming conventions. Example: userID becomes user_id clientID becomes client_id tableName becomes table_name anotherTableName becomes another_table_name etc. Thanks -- Leon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
On 8/11/05, Leon Vismer [EMAIL PROTECTED] wrote: Hi I would like to convert from one naming convention within a sql statement to another. I have the following, code $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'Vismer'); $match = array( /([a-z]+)(ID)/, /([a-z]+)([A-Z])/ ); $replace = array( \$1_id, \$1_\$2 ); ?php $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'Vismer'); $match = '/(?=[a-z])([A-Z]+)/e'; $replace = 'strtolower(_$1)'; print preg_replace($match, $replace, $str); ? insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'Vismer') -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
Hi Robin Many thanks for this, how would one extend this to support the following: $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'mcDonald'); one does not want $str = insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'mc_donald'); unfortunately lookbehind assertions does not support non-fixed length chars so /(?=(?!')[a-z])([A-Z]+)/e will work for 'mDonald' but the following will not work. /(?=(?!')([a-z]+))([A-Z]+)/e Any ideas? Many thanks -- Leon ?php $str = insert into userComment (userID, userName, userSurname) values (0, 'Leon', 'Vismer'); $match = '/(?=[a-z])([A-Z]+)/e'; $replace = 'strtolower(_$1)'; print preg_replace($match, $replace, $str); ? insert into user_comment (user_id, user_name, user_surname) values (0, 'Leon', 'Vismer') -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression help
I just plain suck at regex, so I was hoping to get some hints from you regex experts out there. I'm sure it's been done a thousand times, but I can't seem to find what I'm looking for with a simple google search: $phone could be 1234567890 OR $phone could be 123-456-7890 OR $phone could be (123) 456 7890 OR $phone could have other unexpected characters in it, even control characters. So what I'm desiring to do is run some type of regex on $phone and only return the integers and assign the cleaned string back to $phone. I thought I knew how to do this, but it's not working. Thanks for your guidance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression help
David Christensen wrote: I just plain suck at regex, so I was hoping to get some hints from you regex experts out there. I'm sure it's been done a thousand times, but I can't seem to find what I'm looking for with a simple google search: $phone could be 1234567890 OR $phone could be 123-456-7890 OR $phone could be (123) 456 7890 OR $phone could have other unexpected characters in it, even control characters. So what I'm desiring to do is run some type of regex on $phone and only return the integers and assign the cleaned string back to $phone. I thought I knew how to do this, but it's not working. Thanks for your guidance. If all you want is the digits $phone = preg_replace ( /\D/, , $phone ); Same as /[^0-9]/ -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression to replace pseudo-HTML?
I'm hoping someone can help me figure out a regex that will replace pseudo-HTML codes in a string with desired HTML equivalents. In particular, I'm trying to implement a message quoting facility, such as when you click on the 'quote' button in phpBB. http://us2.php.net/manual/en/function.preg-replace.php There's an example part way down the page that's almost exactly what you want... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression to replace pseudo-HTML?
Hi All, I'm hoping someone can help me figure out a regex that will replace pseudo-HTML codes in a string with desired HTML equivalents. In particular, I'm trying to implement a message quoting facility, such as when you click on the 'quote' button in phpBB. An example of a source string would be: source string [quote=John]Some quoted text. Some more quoted text. [quote=Mary]My older comment.[/quote][/quote] My current comments. /source string Ideally, I'd like this to become something like: regexed string div class='quotehead' John wrote: /div div cass='quote' Some quoted text. Some more quoted text. div class='quotehead' Mary wrote: /div div class='quote' My older comment. /div /div My current comments. /regexed string Given that pseudo-HTML seems common on bulletin board systems I'd hoped there'd be a published function available to handle it, but I haven't had any luck finding one and while I've been able to find a couple of regex examples (eg http://www.regexlib.com/REDetails.aspx?regexp_id=520) they appear to be aimed at ASP.NET rather than PHP and my attempts to get them working in PHP (hampered, admittedly, by my general confusion about regex's) haven't met with any luck. Truly, any help immensely appreciated! Much warmth, Murray
[PHP] Regular expression. What is wrong?
I have to check the phone number. I use some regular expression for this. The phone number can contain only numbers and characters like +,-,),( and space. The problem is when I use a Perl-style all works as intended but when I use a Posix-style I've got the Warning: ereg(): REG_ERANGE and the result is opposite to intended one. What is wrong in the following? ?php $test_phone = 8 (044) 419-0567; if(preg_match(/^[+]?[\d\-\040\)\(]+$/, $test_phone)) { echo valid phonebr /; } else { echo not valid phonebr /; } if(ereg(^[+]?[0-9\-[:space:]\)\(]+$, $test_phone)) { echo valid phonebr /; } else { echo not valid phonebr /; } ? -- Best regards, Kostyantyn Shakhov mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression. What is wrong?
Kostyantyn Shakhov wrote: I have to check the phone number. I use some regular expression for this. The phone number can contain only numbers and characters like +,-,),( and space. The problem is when I use a Perl-style all works as intended but when I use a Posix-style I've got the Warning: ereg(): REG_ERANGE and the result is opposite to intended one. What is wrong in the following? ?php $test_phone = 8 (044) 419-0567; if(preg_match(/^[+]?[\d\-\040\)\(]+$/, $test_phone)) For starters, those \ characters are probably not doing what you think... \ is special in PHP inside quotes (and apostrophes) \ is also special to preg_match. While \d might be fine today, because \d isn't a special combination to PHP, you'd be safer to write \\d so that PHP will ALWAYS send \d to the preg engine. { echo valid phonebr /; } else { echo not valid phonebr /; } if(ereg(^[+]?[0-9\-[:space:]\)\(]+$, $test_phone)) Here, the \- is out of whack. If you want a literal - to be valid, put it at the end of your character class [0-9-] for example, allows 0-9 and the '-' [a-zA-Z0-9-] allows alphanumeric and the '-' I don't think \- is kosher, though, which is your REG_ERANGE error. You may want to check out Regex Coach -- a program that lets you try out Regular Expressions and see them in action so to speak. { echo valid phonebr /; } else { echo not valid phonebr /; } ? -- Best regards, Kostyantyn Shakhov mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression help
Simple functions to check fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() function. Any help is appreciated. ?php /* * ex. 00:AA:11:BB:22:CC */ function chk_mac( $mac ) { if( eregi( ^[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}$, $mac ) ) { return 0; } else { return 1; } } /* * check validity of MAC do replacements if necessary */ function fix_mac( $mac ) { global $mac; /* strip the dash replace with a colon */ if( eregi( ^[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}$, $mac ) ) { $mac = preg_replace( /\-/, :, $mac ); return $mac; } /* add a colon for every two characters */ if( eregi( ^[0-9A-Fa-f]{12}$, $mac ) ) { /* split up the MAC and assign new var names */ @list( $mac1, $mac2, $mac3, $mac4, $mac5, $mac6 ) = @str_split( $mac, 2 ); /* put it back together with the required colons */ $mac = $mac1 . : . $mac2 . : . $mac3 . : . $mac4 . : . $mac5 . : . $mac6; return $mac; } } // do our checks to make sure we are using these damn things right $mac1 = 00aa11bb22cc; $mac2 = 00-aa-11-bb-22-cc; $mac3 = 00:aa:11:bb:22:cc; // make sure it is global global $mac; // if mac submitted is invalid check fix if necessary if( chk_mac( $mac1 ) != 0 ) { $mac = fix_mac( $mac1 ); echo $mac1 . converted to . $mac . br; } if( chk_mac( $mac2 ) != 0 ) { $mac = fix_mac( $mac2 ); echo $mac2 . converted to . $mac . br; } if( chk_mac( $mac3 ) != 0 ) { $mac = fix_mac( $mac3 ); echo $mac3 . converted to . $mac . br; } ? -- Jason Gerfen And remember... If the ladies don't find you handsome, they should at least find you handy... ~The Red Green show -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
On Saturday 22 January 2005 00:12, Jason wrote: Simple functions to check fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() function. Any help is appreciated. Your subject says regular expression help but your problem is with the global variable $mac So which is it? If you're going to declare a variable as 'global' then it has to be done in every function in which that variable is to be used. Which means here: function chk_mac( $mac ) { if( eregi( ^[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa- f]{2}\:[0-9A-Fa-f]{2}$, $mac ) ) { return 0; } else { return 1; } } and wherever else. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- New Year Resolution: Ignore top posted posts -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
On Fri, 2005-01-21 at 10:12, Jason wrote: Simple functions to check fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() function. Any help is appreciated. ?php /* * ex. 00:AA:11:BB:22:CC */ function chk_mac( $mac ) { if( eregi( ^[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}\:[0-9A-Fa-f]{2}$, $mac ) ) { return 0; } else { return 1; } } /* * check validity of MAC do replacements if necessary */ function fix_mac( $mac ) { global $mac; /* strip the dash replace with a colon */ if( eregi( ^[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}\-[0-9A-Fa-f]{2}$, $mac ) ) { $mac = preg_replace( /\-/, :, $mac ); return $mac; } /* add a colon for every two characters */ if( eregi( ^[0-9A-Fa-f]{12}$, $mac ) ) { /* split up the MAC and assign new var names */ @list( $mac1, $mac2, $mac3, $mac4, $mac5, $mac6 ) = @str_split( $mac, 2 ); /* put it back together with the required colons */ $mac = $mac1 . : . $mac2 . : . $mac3 . : . $mac4 . : . $mac5 . : . $mac6; return $mac; } } // do our checks to make sure we are using these damn things right $mac1 = 00aa11bb22cc; $mac2 = 00-aa-11-bb-22-cc; $mac3 = 00:aa:11:bb:22:cc; // make sure it is global global $mac; if you want to use globals you need to use the global in the function not the main body. afaict you don't need globals at all you pass mac1 to each function and use it as $mac in the function (exactly what you want to do) and return local $mac to $mac in the main body. perfectly legal. If you set mac to global in the function I suspect that you end up not using the local mac and are testing an empty var in the first pass of fix_mac. remove the global statements and you are well on your way. Bret // if mac submitted is invalid check fix if necessary if( chk_mac( $mac1 ) != 0 ) { $mac = fix_mac( $mac1 ); echo $mac1 . converted to . $mac . br; } if( chk_mac( $mac2 ) != 0 ) { $mac = fix_mac( $mac2 ); echo $mac2 . converted to . $mac . br; } if( chk_mac( $mac3 ) != 0 ) { $mac = fix_mac( $mac3 ); echo $mac3 . converted to . $mac . br; } ? -- Jason Gerfen And remember... If the ladies don't find you handsome, they should at least find you handy... ~The Red Green show -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
Jason wrote: Simple functions to check fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() function. Any help is appreciated. function fix_mac( $mac ) { global $mac; It's really weird to both pass in $mac as an argument and to declare it global... Do one or the other, but not both. Can't help you with the mess of Regex you've got though... -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
Richard Lynch wrote: Jason wrote: Simple functions to check fix if necessary invalid formating of a MAC address... I seem to be having problems with the global variable $mac not being returned from the fix_mac() function. Any help is appreciated. function fix_mac( $mac ) { global $mac; It's really weird to both pass in $mac as an argument and to declare it global... Do one or the other, but not both. quite. Can't help you with the mess of Regex you've got though... now I bet you can actually, chicken and egg: you have to have the skill to recognise the mess. ;-) Jason - don't be dishearted: a, regexp are/can be hard b, anyone who is giving them a go, is probably trying hard to improve their skills (we like that :-)) - regexps are quite a hurdle for most people. c, we all start out writing regexps like yours (i.e. simple ones) and eventually they become more and more complex/arcane until you become Larry Wall. --- did someone say 'who is Larry Wall?'?, 10 lashes for that man -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression
Hi, This is quite simpal problem that i want to made regular expression which can read this kind of structure... a,b,c not like this 1. ,a,a,a 2. a,,,aa,, 3. a,a,a,,, means simpal structure with comma (a,b,c...) Thnx Ankur -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
Do you even need a regex? What about if (strlen($_POST['mobile_number']) != 11 substr($_POST['mobile_number'],0,3) != 447) { $error=Invalid Number; } On 15 Oct 2004, at 13:38, Shaun wrote: Hi, Could anyone help me with a reugular expression for a UK mobile phone number? So far I have tried this but with no success snip $regexp = /^447[0-9]{9}$/; if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; /snip The number nust be 11 numbers long, be all numbers, have no spaces and begin with 447. Any help would be greatly appreciated... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression for a UK Mobile
Hi, Could anyone help me with a reugular expression for a UK mobile phone number? So far I have tried this but with no success snip $regexp = /^447[0-9]{9}$/; if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; /snip The number nust be 11 numbers long, be all numbers, have no spaces and begin with 447. Any help would be greatly appreciated... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
On Fri, 2004-10-15 at 07:45, Gareth Williams wrote: Do you even need a regex? What about if (strlen($_POST['mobile_number']) != 11 substr($_POST['mobile_number'],0,3) != 447) { $error=Invalid Number; } This doesn't verify that the portion following 447 is also a number. eg. 4474567X901 Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
On Friday 15 October 2004 19:38, Shaun wrote: Could anyone help me with a reugular expression for a UK mobile phone number? So far I have tried this but with no success snip $regexp = /^447[0-9]{9}$/; if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; /snip The number nust be 11 numbers long, be all numbers, have no spaces and begin with 447. 11 minus 3 = 8? -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* When nothing can possibly go wrong, it will. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
Shaun wrote: Hi, Could anyone help me with a reugular expression for a UK mobile phone number? So far I have tried this but with no success snip $regexp = /^447[0-9]{9}$/; if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; /snip The number nust be 11 numbers long, be all numbers, have no spaces and begin with 447. You're matching 447, then 9 numbers after that...bringing the total numbers to 12, not 11. $regexp = /^447\d{8}$/; -- By-Tor.com It's all about the Rush http://www.by-tor.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression for a UK Mobile
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm On 15 October 2004 12:39, Shaun wrote: Hi, Could anyone help me with a reugular expression for a UK mobile phone number? So far I have tried this but with no success snip $regexp = /^447[0-9]{9}$/; That expression looks perfectly valid for the conditions you have stated (given that you meant 12 digits, not 11, which would be correct for UK mobiles). There must be some other kind of error. As a first step, I suggest you do a var_dump($_POST['mobile_number']) to make sure it's exactly what you think it should be. (And, yes, you should be quoting the index to the $_POST[] array.) if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; /snip The number nust be 11 numbers long, be all numbers, have no spaces and begin with 447. However, if you mean genuine mobiles, and not pagers or other follow-me type numbers in the UK 07 range (or even completely unallocated parts of it!), then you need to refine that pattern a bit; this should do: $regexp = /^447[7-9][0-9]{8}$/; Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
Try is_int($_POST['mobile_number']) or ctype_digit($_POST['mobile_number']) HTH Cheers Chris Robert Cummings wrote: On Fri, 2004-10-15 at 07:45, Gareth Williams wrote: Do you even need a regex? What about if (strlen($_POST['mobile_number']) != 11 substr($_POST['mobile_number'],0,3) != 447) { $error=Invalid Number; } This doesn't verify that the portion following 447 is also a number. eg. 4474567X901 Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK Mobile
You could add is_integer() into the if statement. On 15 Oct 2004, at 14:07, Robert Cummings wrote: On Fri, 2004-10-15 at 07:45, Gareth Williams wrote: Do you even need a regex? What about if (strlen($_POST['mobile_number']) != 11 substr($_POST['mobile_number'],0,3) != 447) { $error=Invalid Number; } This doesn't verify that the portion following 447 is also a number. eg. 4474567X901 Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression - highlighting
Hi Michael, Thanks very much for the assistance, I'll have to investigate further! Kind Regards, Aidan Lister Michael Sims [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Aidan Lister wrote: Hello list, I'm pretty terrible with regular expressions, I was wondering if someone would be able to help me with this http://paste.phpfi.com/31964 The problem is detailed in the above link. Basically I need to match the contents of any HTML tag, except a link. I'm pretty sure a lookbehind set is needed in the center (%s) bit. Any suggestions would be appreciated, but it's not quite as simple as it sounds - if possible please make sure you run the above script and see if it PASSED. So basically, you want to put a link around foo, only if it doesn't already have one, right? The problem with look-behind assertions is that they have to be fixed-width. If you're certain of what kind of data you're going to be dealing with then this may be sufficient. For example, I came up with a regex that will PASS your script but I doubt seriously that it'll be very useful to you as it would be easy to break it by coming up with various test cases. For your single test case, however, this works: /(?!a href=foo)(?!a href=)(foo)/ The problem is that HTML tags can be split across lines...they have have any variable amount of whitespace within the tag...they can have other attributes (class, id, onClick), etc. Since look behind assertions have to be fixed width it'd be impossible (IMHO) to come up with a single regex that would match all cases, unless the input data was uniform. For example, stuff like a href = foo ID=id1 class=redlink onClick=javascript:someFunction();foo/a and its infinite variants could not be trapped for with a single regex since you cannot have an infinite number of fixed width look-behind assertions. If quantifying modifiers such as '*', '+', and '?' were allowed in look-behind assertions it would be possible, but they aren't (see man perlre). If your data is coming from unknown sources you'll probably have to use a full fledged HTML parser to pull out text that isn't already part of an a tag. I know there are several of these available for perl and I'm sure there are for PHP too but I'm unaware of them. Sorry if this isn't terribly helpful. Maybe I'm overlooking something and someone else will point out a simple way to accomplish what you're trying to do... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression
How can I grap number written in specifec place in html I try this : preg_match_all(/ TR TD bgColor=#e4e4e4 PBtest:\/B\/P\/TD TD P(*[0-9])\/P\/TD\/TR TR/,$html,$match); its not work How can I do that -- Alawi Albaity Jeddah - KSA Mobile : +966506660442 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
Alawi, You should get some sort of nothing to repeat error. Try moving the * to a + and putting it after the [0-9]. I'm not sure that the * is picking up anything, because it's not preceded by anything. It could be that your $html doesn't match quite right, too (tabs and newlines differ from $html to the regular expression). So I tried this with success: $html =EOB TRTD bgColor=#e4e4e4PBtest:/B/P/TD TDP98938/P/TD/TRTR EOB; preg_match_all(/TRTD bgColor=#e4e4e4PBtest:\/B\/P\/TD TDP([0-9]*)\/P\/TD\/TRTR/,$html,$match); print_r($match); Hans On Thu, 7 Oct 2004, Alawi Albaity wrote: How can I grap number written in specifec place in html I try this : preg_match_all(/ TR TD bgColor=#e4e4e4 PBtest:\/B\/P\/TD TD P(*[0-9])\/P\/TD\/TR TR/,$html,$match); its not work How can I do that -- Alawi Albaity Jeddah - KSA Mobile : +966506660442 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression
On Thu, 7 Oct 2004 12:12:10 -0500 (CDT), Hans H. Anderson [EMAIL PROTECTED] wrote: It could be that your $html doesn't match quite right, too (tabs and newlines differ from $html to the regular expression). So I tried this with success: The 's' modifier will assist with matching across newlines: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php -- Greg Donald Zend Certified Engineer http://gdconsultants.com/ http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression - highlighting
Aidan Lister wrote: Hello list, I'm pretty terrible with regular expressions, I was wondering if someone would be able to help me with this http://paste.phpfi.com/31964 The problem is detailed in the above link. Basically I need to match the contents of any HTML tag, except a link. I'm pretty sure a lookbehind set is needed in the center (%s) bit. Any suggestions would be appreciated, but it's not quite as simple as it sounds - if possible please make sure you run the above script and see if it PASSED. So basically, you want to put a link around foo, only if it doesn't already have one, right? The problem with look-behind assertions is that they have to be fixed-width. If you're certain of what kind of data you're going to be dealing with then this may be sufficient. For example, I came up with a regex that will PASS your script but I doubt seriously that it'll be very useful to you as it would be easy to break it by coming up with various test cases. For your single test case, however, this works: /(?!a href=foo)(?!a href=)(foo)/ The problem is that HTML tags can be split across lines...they have have any variable amount of whitespace within the tag...they can have other attributes (class, id, onClick), etc. Since look behind assertions have to be fixed width it'd be impossible (IMHO) to come up with a single regex that would match all cases, unless the input data was uniform. For example, stuff like a href = foo ID=id1 class=redlink onClick=javascript:someFunction();foo/a and its infinite variants could not be trapped for with a single regex since you cannot have an infinite number of fixed width look-behind assertions. If quantifying modifiers such as '*', '+', and '?' were allowed in look-behind assertions it would be possible, but they aren't (see man perlre). If your data is coming from unknown sources you'll probably have to use a full fledged HTML parser to pull out text that isn't already part of an a tag. I know there are several of these available for perl and I'm sure there are for PHP too but I'm unaware of them. Sorry if this isn't terribly helpful. Maybe I'm overlooking something and someone else will point out a simple way to accomplish what you're trying to do... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression - highlighting
Hello list, I'm pretty terrible with regular expressions, I was wondering if someone would be able to help me with this http://paste.phpfi.com/31964 The problem is detailed in the above link. Basically I need to match the contents of any HTML tag, except a link. I'm pretty sure a lookbehind set is needed in the center (%s) bit. Any suggestions would be appreciated, but it's not quite as simple as it sounds - if possible please make sure you run the above script and see if it PASSED. Here's a little gui to make it easier to test: http://aidan.dotgeek.org/test_hl.php Thanks in advance, Aidan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
On Wednesday 29 September 2004 08:46, Ed Lazor wrote: complain Today I discovered that my ISP can't upgrade to PHP 5. They use Plesk for server Administration and PHP 5 apparently breaks Plesk. Plesk says they'll make PHP 5 support available as soon as it starts coming default on RedHat Enterprise. /complain Unfortunately, I now have a bunch of scripts that require PHP 5. I'd upgraded my beta server for testing and everything has been going so great that I went gungho. Honestly, I tried to not too many of the new features, just so I could ease into it and do more testing. I'm finding little differences that I didn't even realize I was taking advantage of. For example, there are a lot of places in the code where I've done something like this: print Learn more about {$product-get_Title()}.br; http://www.php.net/manual/en/function.preg-replace.php $pattern = /\{\$(.+?)\}/i; $replacement = \\.\$$1\.\; Try this out ;-) I haven't tested it but it's the right direction ;-) I uploaded scripts like this to the production server for more testing and PHP4 flagged all of the code like this as parse errors. I'm not sure, but now I'm stuck having to go through all of the code to change the coding style my editor (Dreamweaver MX 2004) allows me to do a global search and replace using regular expressions. I have no idea what regular expression I'd use for something like this. Any recommendations? Thanks, Ed -- Cyberly yours, Petar Nedyalkov Devoted Orbitel Fan :-) - Orbitel - the New Generation Telecom! See www.orbitel.bg. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] regular expression help
Thanks to everyone who sent in patterns =) They worked like a charm =) $pattern = /\{\$(.+?)\}/i; $replacement = \\.\$$1\.\; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression help
complain Today I discovered that my ISP can't upgrade to PHP 5. They use Plesk for server Administration and PHP 5 apparently breaks Plesk. Plesk says they'll make PHP 5 support available as soon as it starts coming default on RedHat Enterprise. /complain Unfortunately, I now have a bunch of scripts that require PHP 5. I'd upgraded my beta server for testing and everything has been going so great that I went gungho. Honestly, I tried to not too many of the new features, just so I could ease into it and do more testing. I'm finding little differences that I didn't even realize I was taking advantage of. For example, there are a lot of places in the code where I've done something like this: print Learn more about {$product-get_Title()}.br; I uploaded scripts like this to the production server for more testing and PHP4 flagged all of the code like this as parse errors. I'm not sure, but now I'm stuck having to go through all of the code to change the coding style my editor (Dreamweaver MX 2004) allows me to do a global search and replace using regular expressions. I have no idea what regular expression I'd use for something like this. Any recommendations? Thanks, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regular expression help
Howdy, Regular expressions are a simple way of matching patters. You can learn more about regular expressions in general here: http://www.opengroup.org/onlinepubs/007908799/xbd/re.html If you are interested in using regular expressions in PHP, check out these sites: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php A regex to match what you are looking for would be /\{(.+?)\}/, and then just replace it without the { and } for the replace part. On Tue, 2004-09-28 at 22:46, Ed Lazor wrote: complain Today I discovered that my ISP can't upgrade to PHP 5. They use Plesk for server Administration and PHP 5 apparently breaks Plesk. Plesk says they'll make PHP 5 support available as soon as it starts coming default on RedHat Enterprise. /complain Unfortunately, I now have a bunch of scripts that require PHP 5. I'd upgraded my beta server for testing and everything has been going so great that I went gungho. Honestly, I tried to not too many of the new features, just so I could ease into it and do more testing. I'm finding little differences that I didn't even realize I was taking advantage of. For example, there are a lot of places in the code where I've done something like this: print Learn more about {$product-get_Title()}.br; I uploaded scripts like this to the production server for more testing and PHP4 flagged all of the code like this as parse errors. I'm not sure, but now I'm stuck having to go through all of the code to change the coding style my editor (Dreamweaver MX 2004) allows me to do a global search and replace using regular expressions. I have no idea what regular expression I'd use for something like this. Any recommendations? Thanks, Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression: Markup Code
Hello, would someone please help me with my regular expressions? They are so complex! Here is what I need to do. I have a string with contents similar to the following: BLOCK NAME=TOP (a bunch of markup code) /BLOCK BLOCK NAME=BOTTOM (a bunch of markup code) /BLOCK BLOCK NAME=WAYOVERHERE (a bunch of markup code) /BLOCK In other words, the name of the block is not known at runtime. They could be anything really! I need a way to parse through this string, and make new strings with names like $TOP, $BOTTOM, $WAYOVERHERE, that contain their markup code, but not the BLOCK tags. Any help would be greatly appreciated! -Samuel
Re: [PHP] Regular Expression: Markup Code
Hi, Tuesday, September 14, 2004, 6:04:44 AM, you wrote: r Hello, would someone please help me with my regular expressions? They are so r complex! r Here is what I need to do. I have a string with contents similar to the r following: r BLOCK NAME=TOP r(a bunch of markup code) r /BLOCK r In other words, the name of the block is not known at runtime. They could be r anything really! r I need a way to parse through this string, and make new strings with names r like $TOP, $BOTTOM, $WAYOVERHERE, that contain their markup code, but not the r BLOCK tags. r Any help would be greatly appreciated! r -Samuel This should get you started: ? function callback($a){ print_r($a); } $str = END BLOCK NAME=TOP (a bunch of markup code) /BLOCK BLOCK NAME=BOTTOM (a bunch of markup code) /BLOCK BLOCK NAME=WAYOVERHERE (a bunch of markup code) /BLOCK END; preg_replace_callback('!BLOCK NAME=(.*?)(.*?)/BLOCK!is','callback',$str); Use the call back function to build whatever you need. -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression help
I'm trying to replace all occurances of the X character in a text with Y, but only those X's that occur between bold tags (b/b). -- Romanian Web Developers - http://ROWD.ORG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression help
From: Skippy [EMAIL PROTECTED] I'm trying to replace all occurances of the X character in a text with Y, but only those X's that occur between bold tags (b/b). ?php $str = 'This X and this X will not be fixed, but bthis X and/b and hopefully this bX/b should be, along b with this X also. /b, but not this X.'; function myreplace($match) { $from = 'X'; $to = 'Y'; return str_replace($from,$to,$match[0]); } $new_str = preg_replace_callback('#b.*/b#Uis','myreplace',$str); echo $new_str; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression help
Quoting John Holmes [EMAIL PROTECTED]: From: Skippy [EMAIL PROTECTED] I'm trying to replace all occurances of the X character in a text with Y, but only those X's that occur between bold tags (b/b). ?php $str = 'This X and this X will not be fixed, but bthis X and/b and hopefully this bX/b should be, along b with this X also. /b, but not this X.'; function myreplace($match) { $from = 'X'; $to = 'Y'; return str_replace($from,$to,$match[0]); } $new_str = preg_replace_callback('#b.*/b#Uis','myreplace',$str); echo $new_str; ? It works great, thank you. I had tried something on my own previously, but without the ungreedy modifier and with replacements inside a repeated while{} cycle instead of a callback. The main drawback in my version was that an X near hopefully would have been replaced as well (since technically it is between b and /b). But that wasn't what I wanted, of course. -- Romanian Web Developers - http://ROWD.ORG -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression help
I'm trying to figure out how to formulate a regular expression that will get me everything following a pound sign (#) up to the first space or { character. (I'm trying to parse the ids out of a style sheet.) Can anyone point me in the right direction? I've been searching on the web for hours and reading everything I can find on regular expressions, but I just can't wrap my brain around it. Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression help
Daniel Lahey wrote: I'm trying to figure out how to formulate a regular expression that will get me everything following a pound sign (#) up to the first space or { character. (I'm trying to parse the ids out of a style sheet.) Can anyone point me in the right direction? I've been searching on the web for hours and reading everything I can find on regular expressions, but I just can't wrap my brain around it. Thanks. preg_match_all('/#([^\s{]+)/',$style_sheet,$matches); $matches[1] should have all of the matches... -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression help
On Tue, 24 Aug 2004 23:53:53 -0700, Daniel Lahey [EMAIL PROTECTED] wrote: I'm trying to figure out how to formulate a regular expression that will get me everything following a pound sign (#) up to the first space or { character. (I'm trying to parse the ids out of a style sheet.) Can anyone point me in the right direction? I've been searching on the web for hours and reading everything I can find on regular expressions, but I just can't wrap my brain around it. Thanks. Try this snippet pre ?php $css = fopen(http://www.csszengarden.com//001/001.css;, r); $data = fread($css, 3); preg_match_all(/#.+({ )/, $data, $out); print_r($out); ? /pre It gets the default css from the zengarden site. (30k is a large buffer, but this is not a howto on buffered reads) The regular expression /#.+({ )/ means, piece by piece # the # character . dot is a special character in regular expressions. it can match any character + is also a special character, it means match 1 or more times so .+ means match any character 1 or more times ( ) are special characters, they match any single character inside them. in the case of ({ ) , it matches either a space or a left bracket So, the whole expression means: match a strings where they start with #, followed by 1 or more characters, followed by a space or a left-bracket Note that these are very simplified explanations for regular expressions. Now, try the expression /#(.+)({ )/ and instead of print_r($out), try print_r($out[1]); Now you get the classnames :) Check out the following sites for more info (they appear first when you google regular expressions). http://sitescooper.org/tao_regexps.html http://etext.lib.virginia.edu/helpsheets/regex.html Also, check out the PHP manual for the preg_* functions --- Note that the script i gave you has problems with classnames on one line, like #footer a:link, #footer a:visited But I won't spoon feed you :) ramil http://ramil.sagum.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] regular expression
Could someone help me with a regular expression? I am not very good at them. I want to find the following pattern inside a file: [%string%] Then extract the string portion to store in array. I would next like to replace those patterns with html code. That's right you guessed it a template engine type program. I thought it was/\[\%[a-z]\%\] but I guess I am wrong. Then what function do I use to extract. Thank you, George
Re: [PHP] regular expression
First of all, you might want to put more than one % probably like %%% Reason is, asp users % % like php uses ? ?. Just a precaution. You almost had the regex right. /%[a-z]+%/i Thans means, starts with a %, can match a-z, at least once (the + part) but as many times (greedy), then another %, and the i means case-insensitive. If you don't have that you'd have to do [a-zA-Z] instead. Also, will there be anything else in there besides just alpha chars? -Josh On Fri, 2 Jul 2004 13:56:29 -0500, George Lantz [EMAIL PROTECTED] wrote: Could someone help me with a regular expression? I am not very good at them. I want to find the following pattern inside a file: [%string%] Then extract the string portion to store in array. I would next like to replace those patterns with html code. That's right you guessed it a template engine type program. I thought it was/\[\%[a-z]\%\] but I guess I am wrong. Then what function do I use to extract. Thank you, George -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression
Hi All (B (Bwill anyone give me a solution to get the name and email address of sender from the (Bmail text below using regular expression. (B (BThe result shud get (Bname as syed ghouse (Band (Bemail as [EMAIL PROTECTED] (B (B--- Mail text start --- (B (BReturn-Path: [EMAIL PROTECTED] (BDelivered-To: [EMAIL PROTECTED] (BReceived: (qmail 25523 invoked by uid 508); 19 Jun 2004 06:23:25 - (BReceived: from localhost (HELO 192.168.90.8) (127.0.0.1) (B by mail.jinis.com with SMTP; 19 Jun 2004 06:23:25 - (BReceived: from 192.168.90.20 (proxying for 192.168.90.85) (B(SquirrelMail authenticated user [EMAIL PROTECTED]) (Bby 192.168.90.8 with HTTP; (BSat, 19 Jun 2004 11:53:25 +0530 (IST) (BMessage-ID: [EMAIL PROTECTED] (BDate: Sat, 19 Jun 2004 11:53:25 +0530 (IST) (BSubject: test (B (BFrom : 'syed ghouse' [EMAIL PROTECTED] (B (BTo: [EMAIL PROTECTED] (BCc: [EMAIL PROTECTED],[EMAIL PROTECTED] (BUser-Agent: SquirrelMail/1.4.2 (BMIME-Version: 1.0 (BContent-Type: text/plain;charset=iso-8859-1 (BContent-Transfer-Encoding: 8bit (BX-Priority: 3 (BImportance: Normal (B (Btest mail ignore (B (B--- Mail text end --- (B (B (BRegards (B (BSyed
[PHP] Regular Expression Help
Hi folks. I'm having a little bit of a stupid moment with a regular expression which I'm hoping someone can lend a hand with. The regular expression I have right now matches strings between 2 and 1000 characters in length that start with one of the following characters: a-z A-Z 0-9 ( ) and where the rest of the string may be made up of the following characters: a-z A-Z 0-9 ( ) _ , . - ' Here's the working regular expresssion: /^[a-zA-Z0-9\(\)]{1}[ a-zA-Z0-9\(\)_\,\.\-\'\]{1,999}$/ Now, I'm trying to add \ to the valid characters but when I add a slash after \ I get the following error: Warning: Compilation failed: missing terminating ] for character class at offset 54 in /u0/vservers/*/html/classes/forms/forms_validation_functions.class.p hp on line 184 So I escape the \ with another \, and I still get the error. Three or four \ results in the \ character being stripped from the string that is being tested with the regular expression. Five \ results in the orignal error which I get with just one \. Can anyone give any advice as to why this is happening? Cheers and TIA. Pablo /^[a-zA-Z0-9\(\)]{1}[ a-zA-Z0-9\(\)_\,\.\-\'\]{1,999}$/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression Help
Howdy folks. Sorry for the message which just showed up truncated. It's complete in my sent mail, but appeared truncated on the list. Full message is below. TIA for all help. Hi folks. I'm having a little bit of a stupid moment with a regular expression which I'm hoping someone can lend a hand with. The regular expression I have right now matches strings between 2 and 1000 characters in length that start with one of the following characters: a-z A-Z 0-9 ( ) and where the rest of the string may be made up of the following characters: a-z A-Z 0-9 ( ) _ , . - ' Here's the working regular expresssion: /^[a-zA-Z0-9\(\)]{1}[ a-zA-Z0-9\(\)_\,\.\-\'\]{1,999}$/ Now, I'm trying to add \ to the valid characters but when I add a slash after \ I get the following error: Warning: Compilation failed: missing terminating ] for character class at offset 54 in /u0/vservers/*/html/classes/forms/forms_validation_functions.class.p hp on line 184 So I escape the \ with another \, and I still get the error. Three or four \ results in the \ character being stripped from the string that is being tested with the regular expression. Five \ results in the orignal error which I get with just one \. Can anyone give any advice as to why this is happening? Cheers and TIA. Pablo -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression Help
* Thus wrote Pablo Gosse: Here's the working regular expresssion: /^[a-zA-Z0-9\(\)]{1}[ a-zA-Z0-9\(\)_\,\.\-\'\]{1,999}$/ for starters, that doesn't give me a warning at all. also, all those escapes arn't needed: $reg = '/^[a-zA-Z0-9()]{1}[ a-zA-Z0-9()_,.\'-]{1,999}$/'; The ' is only escaped for php, not the expression. And moving the - to the end or beginning, you don't need to escape it. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: [PHP] Regular Expression - it works but uses way too much memory ?
Robin Vickery sagde: The S modifier that you're using means that it's storing the studied expression. If the regexp changes each time around the loop then over 3 iterations, that'll add up. See if removing that modifier helps at all. The S modifier wasn't needed, I added it because I thought it would speed it up but it didn't. Removing it didn't help on the memory usage, but it performs a little better without. If that's not it, then these *might* save you some memory, although I've not tested them: I'm not entirely sure why you're matching (.*) at the end then putting it back in with your replacement text. Without running it, I'd have thought that you could leave out the (.*) from your pattern and the $4 from your replacement and get exactly the same effect. I tried removing $4 and (.*) but the result isn't the same, actually my first reg. exp. didn't have $4, but I had to add it. Without it 51 of the 1246 texts isn't processed right? Also there isn't really any difference in how it performs with or without it. You could use a non-capturing subpattern for $2 which you're not using in your replacement. $replace = /^((?:[a-z]+?[^a-z]+?){.($count).})(.$typedmask.)/i; I didn't know you could do that.. cool :), this made the script run a little faster but it still uses the same amount of memory. And maybe a look-behind assertion for the first subpattern rather than a capturing pattern then re-inserting $1. $replace = /^(?=(?:[a-z]+?[^a-z]+?){.($count).})(.$typedmask.)/i; $with = error-start sourcetext=.$corr['sourcetext']. id=.$corr['id']. ... With ?= I get a lot of warnings: here is an example: $replace is '/^(?=(?:[a-z]+?[^a-z]+?){50})(go)(.*)/i' $with is 'error-start sourcetext=3 id=49 group=- class=- corrected-from=go corrected-to=god$2error-end sourcetext=3 id=49$3' br / bWarning/b: Compilation failed: lookbehind assertion is not fixed length at offset 34 with the corrections added the reg.exp. looks like this: $typedmask = preg_replace(/\s+/,.*?,$corr['typed']); $replace = '/^((?:[a-z]+?[^a-z]+?){'.($count).'})('.$typedmask.')(.*)/i'; $with = '$1error-start sourcetext='.$corr['sourcetext'].' id='.$corr['id'].' group='.$corr['grupper'].' class='.$corr['ordklasse'].' corrected-from='.$corr['typed'].' corrected-to='.$corr['corrected'].'$2error-end sourcetext='.$corr['sourcetext'].' id='.$corr['id'].'$3'; $text = $skipText[0] . preg_replace ($replace,$with,$text,1); It completes a little faster and the output is exactly the same as before, but it still uses way too much memory. [EMAIL PROTECTED] testextract]# time php ../export.php export6.txt real1m15.851s user0m18.720s sys 0m1.750s From top just before the script completed: PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND 7843 root 17 0 269M 269M 3328 R41.7 53.6 0:19 php This isn't a huge problem anymore, as we have been allowed to move the project to a 3 times faster server with less activity (because of this). But I would still like to know if there is a solution to this because it seems quite insane that it allocates more than 250MB memory generate 4MB output. Thanks Robin! I really appreciate your answer. Brgds Ulrik -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression - it works but uses way too much memory ?
Sorry to post this again but it's a little urgent. The preg_replace in my script allocates a little memory every time it is called and doesn't free it again untill the script ends. I don't know if it is normal behaviour for preg_replace or if it is my reg. exp. that causes this. The problem is that I call preg_replace a little more than 3 times and that causes quite a lot of memory to be allocated. Doesn't anybody know if this is a problem in preg_replace or is there a better maillist/forum for this kind of questions? Any answer will be appreciated :) Ulrik S. Kofod sagde: $replace = /^(([a-z]+?[^a-z]+?){.($count).})(.$typedmask.)(.*)/iS; $with = $1error-start sourcetext=.$corr['sourcetext']. id=.$corr['id']. group=\.$corr['grupper'].\ class=\.$corr['ordklasse'].\ corrected-from=\.$corr['typed'].\ corrected-to=\.$corr['corrected'].\$3error-end sourcetext=.$corr['sourcetext']. id=.$corr['id'].$4; $text = preg_replace ($replace,$with,$text,1); Above preg_replace works as expected, I have it inside a loop that processes 1000 texts and replaces a total of 3 words. The problem is that it accumulates memory up to 200MB, that isn't freed again until the script completes? It runs for about 1 - 2 minutes and allocates more and more memory. If I comment out the preg_replace the memory usage looks normal, so I'm pretty sure that is where the problem is? My question is basically, is it something in my reg. exp. that it totally nuts or is it normal behaviour for preg_replace to allocate memory and not free it again until the script ends? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: [PHP] Regular Expression - it works but uses way too much memory ?
On Fri, 18 Jun 2004 08:57:19 +0200 (CEST), Ulrik S. Kofod [EMAIL PROTECTED] wrote: Sorry to post this again but it's a little urgent. The preg_replace in my script allocates a little memory every time it is called and doesn't free it again untill the script ends. I don't know if it is normal behaviour for preg_replace or if it is my reg. exp. that causes this. The problem is that I call preg_replace a little more than 3 times and that causes quite a lot of memory to be allocated. $replace = /^(([a-z]+?[^a-z]+?){.($count).})(.$typedmask.)(.*)/iS; $with = $1error-start sourcetext=.$corr['sourcetext']. id=.$corr['id']. group=\.$corr['grupper'].\ class=\.$corr['ordklasse'].\ corrected-from=\.$corr['typed'].\ corrected-to=\.$corr['corrected'].\$3error-end sourcetext=.$corr['sourcetext']. id=.$corr['id'].$4; $text = preg_replace ($replace,$with,$text,1); The problem is that it accumulates memory up to 200MB, that isn't freed again until the script completes? The S modifier that you're using means that it's storing the studied expression. If the regexp changes each time around the loop then over 3 iterations, that'll add up. See if removing that modifier helps at all. $replace = /^(([a-z]+?[^a-z]+?){.($count).})(.$typedmask.)(.*)/i; If that's not it, then these *might* save you some memory, although I've not tested them: I'm not entirely sure why you're matching (.*) at the end then putting it back in with your replacement text. Without running it, I'd have thought that you could leave out the (.*) from your pattern and the $4 from your replacement and get exactly the same effect. $replace = /^(([a-z]+?[^a-z]+?){.($count).})(.$typedmask.)/i; ... sourcetext=.$corr['sourcetext']. id=.$corr['id'].; You could use a non-capturing subpattern for $2 which you're not using in your replacement. $replace = /^((?:[a-z]+?[^a-z]+?){.($count).})(.$typedmask.)/i; And maybe a look-behind assertion for the first subpattern rather than a capturing pattern then re-inserting $1. $replace = /^(?=(?:[a-z]+?[^a-z]+?){.($count).})(.$typedmask.)/i; $with = error-start sourcetext=.$corr['sourcetext']. id=.$corr['id']. ... -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression - it works but uses way too much memory ?
$replace = /^(([a-z]+?[^a-z]+?){.($count).})(.$typedmask.)(.*)/iS; $with = $1error-start sourcetext=.$corr['sourcetext']. id=.$corr['id']. group=\.$corr['grupper'].\ class=\.$corr['ordklasse'].\ corrected-from=\.$corr['typed'].\ corrected-to=\.$corr['corrected'].\$3error-end sourcetext=.$corr['sourcetext']. id=.$corr['id'].$4; $text = preg_replace ($replace,$with,$text,1); Above preg_replace works as expected, I have it inside a loop that processes 1000 texts and replaces a total of 3 words. The problem is that it accumulates memory up to 200MB, that isn't freed again until the script completes? It runs for about 1 - 2 minutes and allocates more and more memory. If I comment out the preg_replace the memory usage looks normal, so I'm pretty sure that is where the problem is? My question is basically, is it something in my reg. exp. that it totally nuts or is it normal behaviour for preg_replace to allocate memory and not free it again until the script ends? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular expression question
So I'm trying to implement a simple wiki-like syntax for hyperlinking. Basically I want to match stuff like [this], where the word 'this' gets turned into a hyperlink. I have that working, but I want to be able to escape the opening bracket, so that it's possible to do \[that] without having it match as a link. Here's what I've got: // Matches fine, but without escaping $pattern = / \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Throws an unmatched bracket warning $pattern = / [^\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Ignores escaping: \[example] still matches $pattern = / [^\\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; Nothing seems to change if I keep adding backslashes to that first matching thingy (i.e. the escaping still doesn't work). Any ideas? Thanks much, -Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
On Thu, May 27, 2004 at 09:59:05AM -0700, Dan Phiffer wrote: So I'm trying to implement a simple wiki-like syntax for hyperlinking. Basically I want to match stuff like [this], where the word 'this' gets turned into a hyperlink. I have that working, but I want to be able to escape the opening bracket, so that it's possible to do \[that] without having it match as a link. Here's what I've got: // Matches fine, but without escaping $pattern = / \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Throws an unmatched bracket warning $pattern = / [^\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Ignores escaping: \[example] still matches $pattern = / [^\\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; Nothing seems to change if I keep adding backslashes to that first matching thingy (i.e. the escaping still doesn't work). Any ideas? Try negative lookbehinds... $pattern = ' / (?!) \[# open [ not preceded by a backslash (.*?) # ungreedy match anything (?!) \]# close ] not preceded by a backslash /x '; - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
Rob Ellis wrote: On Thu, May 27, 2004 at 09:59:05AM -0700, Dan Phiffer wrote: So I'm trying to implement a simple wiki-like syntax for hyperlinking. Basically I want to match stuff like [this], where the word 'this' gets turned into a hyperlink. I have that working, but I want to be able to escape the opening bracket, so that it's possible to do \[that] without having it match as a link. Here's what I've got: // Matches fine, but without escaping $pattern = / \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Throws an unmatched bracket warning $pattern = / [^\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Ignores escaping: \[example] still matches $pattern = / [^\\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; Nothing seems to change if I keep adding backslashes to that first matching thingy (i.e. the escaping still doesn't work). Any ideas? Try negative lookbehinds... $pattern = ' / (?!) \[# open [ not preceded by a backslash (.*?) # ungreedy match anything (?!) \]# close ] not preceded by a backslash /x '; - Rob BTW, instead of un-greedy maych anything (.*?) You could use a negative groupof course to deal with \] as well you have to do alittle more: ([^\]]|[\]])* -- paperCrane Justin Patrin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression
Why isn't my regex working? From everything that I've read, it should be... script language=php $string = [joebob]; if( preg_match( '/\[(\s+)\]/i', $string, $aMatches )) { print_r( $aMatches ); } else { echo 'No match was found' . \n\n; } /script Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression
Chris Boget mailto:[EMAIL PROTECTED] on Tuesday, May 18, 2004 9:55 AM said: Why isn't my regex working? From everything that I've read, it should be... $string = [joebob]; if( preg_match( '/\[(\s+)\]/i', $string, $aMatches )) { \s Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v]. [1] hth, chris. [1] http://www.amk.ca/python/howto/regex/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression
Chris Boget wrote: Why isn't my regex working? From everything that I've read, it should be... script language=php $string = [joebob]; if( preg_match( '/\[(\s+)\]/i', $string, $aMatches )) { print_r( $aMatches ); } else { echo 'No match was found' . \n\n; } /script Chris your regex is matching one or more whitespace characters between [ and ]. i think you wanted \S and not \s. see: http://www.php.net/manual/en/pcre.pattern.syntax.php Ryan Briones -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Regular Expression
Here's a neat little tool I came across while taking an ASP.NET course at a local college for creating regular expressions. I've used it with my Perl/PHP scripting also. Regular Expression Designer http://www.radsoftware.com.au/web/Default.aspx Stanley G. Martin System Administrator Sprint - EAS Business Intelligence [EMAIL PROTECTED] -Original Message- From: Tumurbaatar S. [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 05, 2004 12:26 AM To: [EMAIL PROTECTED] Subject: [PHP] Regular Expression There's an input string like {str1,str2,...,strN}. I want to capture all these strings and due to complexity of them I use a preg_match_all() instead of simple split. A pattern for the matching strings is ready but I cannot imagine how to specify that strings are separated by commas and the last one is not followed by comma. For example, I'm afraid that this pattern /^{(?:(pattern),)*|(pattern)?}$/ can match and capture properly constructed input string, but, in addition, this matches if the string end is ,}. Any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression
There's an input string like {str1,str2,...,strN}. I want to capture all these strings and due to complexity of them I use a preg_match_all() instead of simple split. A pattern for the matching strings is ready but I cannot imagine how to specify that strings are separated by commas and the last one is not followed by comma. For example, I'm afraid that this pattern /^{(?:(pattern),)*|(pattern)?}$/ can match and capture properly constructed input string, but, in addition, this matches if the string end is ,}. Any ideas? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Regular Expression for a UK mobile phone number
Hi, I am trying to create a regular expression for a mobile phone number. The number must be 12 digits long(0-9) and begin with 447 and have no spaces. So far I have come up with this but it keeps telling me the number is invalid even when its correct! $regexp = /447[0-9]{9}/; if($_POST[mobile_number] != ''){ if(preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; header(Location: edit_rep.php?error=$errorrep_id=.$_GET[rep_id].client_id=.$_GET[client_ id].rep_name=.$_GET[rep_name].client_name=.$_GET[client_name].); exit; } } I would be most grateful for any advice offered. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular Expression for a UK mobile phone number
I am trying to create a regular expression for a mobile phone number. The number must be 12 digits long(0-9) and begin with 447 and have no spaces. So far I have come up with this but it keeps telling me the number is invalid even when its correct! Try this: $regexp = /447[0-9]{9}/; if($_POST[mobile_number] != ''){ if(!preg_match( $regexp, $_POST[mobile_number] )){ $error = Invalid Mobile Number; header(Location:edit_rep.php?error=$errorrep_id=.$_GET[rep_id].client_id=.$_GET[client_id].rep_name=.$_GET[rep_name].client_name=.$_GET[client_name].); exit; } } Also, your regexp is a little permissive; you can anchor it like so: $regexp = /^447[0-9]{9}$/; - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php