php-general Digest 18 May 2012 13:25:51 -0000 Issue 7816
php-general Digest 18 May 2012 13:25:51 - Issue 7816 Topics (messages 317881 through 317892): Re: regexp novice 317881 by: Jim Giner 317882 by: shiplu 317883 by: Yared Hufkens 317884 by: Jim Giner 317885 by: Govinda 317886 by: Jim Giner 317887 by: Jim Lucas 317888 by: Jim Giner 317889 by: Jim Lucas 317890 by: Jim Lucas 317891 by: shiplu 317892 by: Jim Giner Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- OOPS FORGOT to mention that I modify the string to add a colon if it is entered without one, so my regexp always expects a : to be in the middle. So in actuality - my regexp is 'passing' a value of 13:00 as legitimate, when it should not be. ---End Message--- ---BeginMessage--- On Fri, May 18, 2012 at 2:37 AM, Jim Giner jim.gi...@albanyhandball.comwrote: ok - finally had to come up with my own regexp - and am failing. Trying to validate an input of a time value in the format hh:mm, wherein I'll accept anything like the following: hmm hhmm h:mm hh:mm in a 12 hour format. My problem is my test is ok'ing an input of 1300. Here is my test: if (0 == preg_match(/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/,$t)) return true; else return false; Can someone help me correct my regexp? I can not correct your regexp. But I must tell you that trying to tweak a regex for hours is surely **not productive**. If you got any type of text processing dont always go for regular expression. This problem can be solved just by simple string parsing. Here I have done that for you. function valid_time($time){ $m = (int) substr($time, -2); $h = (int) substr($time, 0, -2); return ($h=0 $h13 $m=0 $m60); } -- Shiplu.Mokadd.im ImgSign.com | A dynamic signature machine Innovation distinguishes between follower and leader ---End Message--- ---BeginMessage--- Try this: /(0?[1-9]|[12][0-9]):?[0-5][0-9]/ FYI: ? is equal to {0,1}, and [1-9] to [123456789] (and therefore [1-2] to [12]). Am 17.05.2012 22:37, schrieb Jim Giner: ok - finally had to come up with my own regexp - and am failing. Trying to validate an input of a time value in the format hh:mm, wherein I'll accept anything like the following: hmm hhmm h:mm hh:mm in a 12 hour format. My problem is my test is ok'ing an input of 1300. Here is my test: if (0 == preg_match(/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/,$t)) return true; else return false; Can someone help me correct my regexp? ---End Message--- ---BeginMessage--- Yared Hufkens y4...@yahoo.de wrote in message news:4fb5667d.7020...@yahoo.de... Try this: /(0?[1-9]|[12][0-9]):?[0-5][0-9]/ FYI: ? is equal to {0,1}, and [1-9] to [123456789] (and therefore [1-2] to [12]). Am 17.05.2012 22:37, schrieb Jim Giner: ok - finally had to come up with my own regexp - and am failing. Trying to validate an input of a time value in the format hh:mm, wherein I'll accept anything like the following: hmm hhmm h:mm hh:mm in a 12 hour format. My problem is my test is ok'ing an input of 1300. Here is my test: if (0 == preg_match(/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/,$t)) return true; else return false; Can someone help me correct my regexp? Nope - that didn't work. Tested it against 1900, 1300 and 13:00 and all came thru as OK. Also - I don't understand at all the following: FYI: ? is equal to {0,1}, and [1-9] to [123456789] (and therefore [1-2] to [12]). I know (?) that [1-9] validates any digit from 1 to 9 - I was already using that. And your point about [1-2] doesn't make sense to me since I need to validate 10:00 which [1-2] in my usage would cause 10:00 to fail. And I don't know what ? means at all. FWIW - I couldn't find much in the way of tutorials on the meanings of the various chars in regexp's. ---End Message--- ---BeginMessage--- FWIW - I couldn't find much in the way of tutorials on the meanings of the various chars in regexp's. this helps alot: http://www.gskinner.com/RegExr/ you can paste your pattern (needle) in the top input, and hover over each char to see what it means in grep land. Paste your haystack in the big box (input), under that, to see where all your needle will be found. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ---End Message--- ---BeginMessage--- Thank you ! Govinda govinda.webdnat...@gmail.com wrote in message news:3e5dce87-29c1-4679-ad3a-53326435f...@gmail.com... FWIW - I couldn't find much in the way of tutorials on the meanings of the various chars in regexp's. this helps
Re: [PHP] regexp novice
Jim Lucas li...@cmsws.com wrote in message news:4fb5decc.20...@cmsws.com... On 5/17/2012 9:52 PM, Jim Lucas wrote: How about this instead? pre?php $times = array( '100', # valid '1100', # valid '1300', # invalid '01:00', # valid '12:59', # valid '00:01', # valid '00:25pm', # invalid '', # valid 'a00', # invalid '00', # invalid ); foreach ( $times AS $time ) echo {$time} is .(valid_date($time)?'valid':'invalid').\n; function valid_date($time) { if ( ( $c_time = preg_replace('|[^\d\:]+|', '', $time) ) != $time ) return false; preg_match('#^(?Phour\d{1,2}):?(?Pminute\d{2})$#', $time, $m); if ( $m ( 0 = (int) $m['hour'] 12 = (int) $m['hour'] ) ( 0 = (int) $m['minute'] 59 = (int) $m['minute'] ) ) { return TRUE; } return false; } Let me know. I optimized it a little... http://www.cmsws.com/examples/php/testscripts/shiplu@gmail.com/pt_regex.php http://www.cmsws.com/examples/php/testscripts/shiplu@gmail.com/pt_regex.phps pre?php $times = array( '100', # valid '1100', # valid '1300', # invalid '01:00',# valid '12:59',# valid '00:01',# valid '00:25pm', # invalid '', # valid 'a00', # invalid '00', # invalid ); foreach ( $times AS $time ) echo {$time} is .(valid_time($time)?'valid':'invalid').\n; function valid_time($time) { if ( preg_match('#^(\d{1,2}):?(\d{2})$#', $time, $m) ( 0 = (int) $m[1] 12 = (int) $m[1] ) ( 0 = (int) $m[2] 59 = (int) $m[2] ) ) { return TRUE; } return FALSE; } I'll have to study your regexp - a lot of stuff I don't understand yet in play there. Thanks for the sample! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
Jim Lucas li...@cmsws.com wrote in message news:4fb5decc.20...@cmsws.com... On 5/17/2012 9:52 PM, Jim Lucas wrote: How about this instead? pre?php $times = array( '100', # valid '1100', # valid '1300', # invalid '01:00', # valid '12:59', # valid '00:01', # valid '00:25pm', # invalid '', # valid 'a00', # invalid '00', # invalid ); foreach ( $times AS $time ) echo {$time} is .(valid_date($time)?'valid':'invalid').\n; function valid_date($time) { if ( ( $c_time = preg_replace('|[^\d\:]+|', '', $time) ) != $time ) return false; preg_match('#^(?Phour\d{1,2}):?(?Pminute\d{2})$#', $time, $m); if ( $m ( 0 = (int) $m['hour'] 12 = (int) $m['hour'] ) ( 0 = (int) $m['minute'] 59 = (int) $m['minute'] ) ) { return TRUE; } return false; } Let me know. I optimized it a little... http://www.cmsws.com/examples/php/testscripts/shiplu@gmail.com/pt_regex.php http://www.cmsws.com/examples/php/testscripts/shiplu@gmail.com/pt_regex.phps pre?php $times = array( '100', # valid '1100', # valid '1300', # invalid '01:00',# valid '12:59',# valid '00:01',# valid '00:25pm', # invalid '', # valid 'a00', # invalid '00', # invalid ); foreach ( $times AS $time ) echo {$time} is .(valid_time($time)?'valid':'invalid').\n; function valid_time($time) { if ( preg_match('#^(\d{1,2}):?(\d{2})$#', $time, $m) ( 0 = (int) $m[1] 12 = (int) $m[1] ) ( 0 = (int) $m[2] 59 = (int) $m[2] ) ) { return TRUE; } return FALSE; } OK - I don't yet understand how this works, but it seems to work for almost all cases. The one erroneous result I get is from a value of 0040 (which I convert to 00:40 before hitting the regexp). It comes thru as Ok. If you have a fix for that I'd appreciate it - otherwise I'll have to devote some book-time to mastering this string and come up with a fix myself. Thanks again!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
On 18 May 2012, at 14:32, Jim Giner wrote: OK - I don't yet understand how this works, but it seems to work for almost all cases. The one erroneous result I get is from a value of 0040 (which I convert to 00:40 before hitting the regexp). It comes thru as Ok. If you have a fix for that I'd appreciate it - otherwise I'll have to devote some book-time to mastering this string and come up with a fix myself. Based on your requirements, 00:40 is completely valid. Why do you think it should be invalid? -- Stuart Dallas 3ft9 Ltd http://3ft9.com/
Re: [PHP] regexp novice
On Fri, May 18, 2012 at 7:34 PM, Stuart Dallas stu...@3ft9.com wrote: Based on your requirements, 00:40 is completely valid. Why do you think it should be invalid? 00:40 is not a valid 12-hour format. BTW I just found another non-regex approach. Its even faster. function valid_time_Shiplu2($time) { sscanf($time, %2d%2d, $h, $m); return ($h0 $h13 $m=0 $m60); } -- Shiplu.Mokadd.im ImgSign.com | A dynamic signature machine Innovation distinguishes between follower and leader
Re: [PHP] regexp novice
Stuart Dallas stu...@3ft9.com wrote in message news:cc22e241-c1df-48e9-bf06-8a638a356...@3ft9.com... On 18 May 2012, at 14:32, Jim Giner wrote: OK - I don't yet understand how this works, but it seems to work for almost all cases. The one erroneous result I get is from a value of 0040 (which I convert to 00:40 before hitting the regexp). It comes thru as Ok. If you have a fix for that I'd appreciate it - otherwise I'll have to devote some book-time to mastering this string and come up with a fix myself. Based on your requirements, 00:40 is completely valid. Why do you think it should be invalid? -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ Don't know how you write the time, but I've never used a time of 00:40. Yes, I realize that my shorthand time string is missing a key ingredient of am/pm, but 12:40 would be the time in my mind regardless of the status of the sun. In my speccific use of this code, all times would be 'daylight' times so 40 minutes after minute would be a) not practical and b) still not a recognized time in a 12-hour format. Yes - in 24-hour formats, 00:40 is correct, but my initial post did reference my need of a 12-hour format solution. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
On 18 May 2012, at 14:41, Jim Giner wrote: Stuart Dallas stu...@3ft9.com wrote in message news:cc22e241-c1df-48e9-bf06-8a638a356...@3ft9.com... On 18 May 2012, at 14:32, Jim Giner wrote: OK - I don't yet understand how this works, but it seems to work for almost all cases. The one erroneous result I get is from a value of 0040 (which I convert to 00:40 before hitting the regexp). It comes thru as Ok. If you have a fix for that I'd appreciate it - otherwise I'll have to devote some book-time to mastering this string and come up with a fix myself. Based on your requirements, 00:40 is completely valid. Why do you think it should be invalid? Don't know how you write the time, but I've never used a time of 00:40. Yes, I realize that my shorthand time string is missing a key ingredient of am/pm, but 12:40 would be the time in my mind regardless of the status of the sun. In my speccific use of this code, all times would be 'daylight' times so 40 minutes after minute would be a) not practical and b) still not a recognized time in a 12-hour format. Yes - in 24-hour formats, 00:40 is correct, but my initial post did reference my need of a 12-hour format solution. Sounds daft to me, but they're your requirements. The fix is simple… ( 0 = (int) $m[1] 12 = (int) $m[1] ) becomes ( 1 = (int) $m[1] 12 = (int) $m[1] ) -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
times so 40 minutes after minute would be a) not practical and b) still not I meant to say 40 minutes after MIDNIGHT. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
Stuart Dallas stu...@3ft9.com wrote in message news:79538829-bfc4-43a4-a413-72247b145...@3ft9.com... On 18 May 2012, at 14:41, Jim Giner wrote: Stuart Dallas stu...@3ft9.com wrote in message news:cc22e241-c1df-48e9-bf06-8a638a356...@3ft9.com... On 18 May 2012, at 14:32, Jim Giner wrote: OK - I don't yet understand how this works, but it seems to work for almost all cases. The one erroneous result I get is from a value of 0040 (which I convert to 00:40 before hitting the regexp). It comes thru as Ok. If you have a fix for that I'd appreciate it - otherwise I'll have to devote some book-time to mastering this string and come up with a fix myself. Based on your requirements, 00:40 is completely valid. Why do you think it should be invalid? Don't know how you write the time, but I've never used a time of 00:40. Yes, I realize that my shorthand time string is missing a key ingredient of am/pm, but 12:40 would be the time in my mind regardless of the status of the sun. In my speccific use of this code, all times would be 'daylight' times so 40 minutes after minute would be a) not practical and b) still not a recognized time in a 12-hour format. Yes - in 24-hour formats, 00:40 is correct, but my initial post did reference my need of a 12-hour format solution. Sounds daft to me, but they're your requirements. The fix is simple ( 0 = (int) $m[1] 12 = (int) $m[1] ) becomes ( 1 = (int) $m[1] 12 = (int) $m[1] ) -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/= Daft is a little harsh. :) 00:40 is just not a time value that is generally accepted. As for you patch thought - THAT is generally accepted. Works great now. Thank you. Now all I have to do is read up on this stuff so I can understand how it works. But first - golf! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
On 18 May 2012, at 14:50, Jim Giner wrote: Daft is a little harsh. :) 00:40 is just not a time value that is generally accepted. It may appear harsh, but as far as I'm concerned it is daft to make assumptions like that. You've essentially disallowed 12:nn am, but allowed 1:nn am, 2:nn am, 3:nn am, etc, because you're not validating the data in a non-ambiguous way. I have no idea what you're developing, but you're making a big assumption about the data that you're getting, which may appear reasonable to you, but to me it's daft. Nothing personal, just my opinion, which is all I have to offer. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/
[PHP] Bust out a PDF via the print stylesheet?
Hey all - The articles on my web site already have a very nice stylesheet that produces a print version. Does anyone know if there's a such a thing as a PHP class that would let me put up a Download PDF link that would generate a PDF doc on the fly, using that same stylesheet? I've used various PHP PDF classes before, but I'd rather see if I can shortcut this rather than assembling the doc from scratch. - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] FPDF ?
I never found a solution to this myself. On Apr 26, 2012, at 2:13 PM, Jim Giner wrote: For those of you with FPDF experience. I've just begun using it and have figured out how it works I think. I am still having trouble with the bottom of the page tho. Seems that if I get too close to the bottom margin and my data line exceeds the amount of available space, my MultiCell elements print some of their contents and then my Footer gets printed and then I go to a new page where some small amount of the remaining data for that line gets printed and then a new page is output and repeat. This can go on for 3-4 pages before things work out and my report continues until it gets a full page again and then it all happens again. I know it sounds complicated, but I'm hoping someone else has experienced this kind of learning curve and can give me a clue as to what I'm doing wrong, or at least what's happening. Even better would be an algorithm for detecting how much space I have left so I can avoid these split lines and perhaps solve my entire problem. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Bust out a PDF via the print stylesheet?
Bastien Koert On 2012-05-18, at 12:34 PM, Brian Dunning br...@briandunning.com wrote: Hey all - The articles on my web site already have a very nice stylesheet that produces a print version. Does anyone know if there's a such a thing as a PHP class that would let me put up a Download PDF link that would generate a PDF doc on the fly, using that same stylesheet? I've used various PHP PDF classes before, but I'd rather see if I can shortcut this rather than assembling the doc from scratch. - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Have a look at dompdf. It's a class that allows you to turn the HTML into a PDF Bastien -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
On 2012-05-17 22:37, Jim Giner wrote: Trying to validate an input of a time value in the format hh:mm, wherein I'll accept anything like the following: hmm hhmm h:mm hh:mm in a 12 hour format. My problem is my test is ok'ing an input of 1300. Here is my test: if (0 == preg_match(/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/,$t)) return true; else return false; Can someone help me correct my regexp? /([0][1-9]|[1][0-2]|^[1-9]):[0-5][0-9]/ The third part of your alternate expressions matches 3:00 from the string 13:00 (it ignores the 1 at the beginning). Using ^ avoids this because now only one digit is allowed before the :. Bye, Andreas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
Stuart Dallas stu...@3ft9.com wrote in message news:aba011df-8cdf-4492-be4d-51c2b54c4...@3ft9.com... On 18 May 2012, at 14:50, Jim Giner wrote: Daft is a little harsh. :) 00:40 is just not a time value that is generally accepted. It may appear harsh, but as far as I'm concerned it is daft to make assumptions like that. You've essentially disallowed 12:nn am, but allowed 1:nn am, 2:nn am, 3:nn am, etc, because you're not validating the data in a non-ambiguous way. I have no idea what you're developing, but you're making a big assumption about the data that you're getting, which may appear reasonable to you, but to me it's daft. Nothing personal, just my opinion, which is all I have to offer. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ Ok - here's the use. This feature is the scheduling portion of my application. The scheduling only pertains to basically daytime hours, typically 8:00am to 6:00pm. The information is used for display purposes mostly - there is no calculating going on with the data. Consequently, there is no need to be all-inclusive on my allowed times since they will never be used. I just want to validate the entries to be sure that a valid time has been entered for that period of a day. Noone is going to schedule anything for a midnight hour, not even a time after 8:00pm. Therefore I can be very specific about my editing criteria and can limit the entry of data that fits within that schedule. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] regexp novice
On Thu, May 17, 2012 at 3:37 PM, Jim Giner jim.gi...@albanyhandball.com wrote: ok - finally had to come up with my own regexp - and am failing. Trying to validate an input of a time value in the format hh:mm, wherein I'll accept anything like the following: hmm hhmm h:mm hh:mm in a 12 hour format. My problem is my test is ok'ing an input of 1300. Here is my test: if (0 == preg_match(/([0][1-9]|[1][0-2]|[1-9]):[0-5][0-9]/,$t)) return true; else return false; Can someone help me correct my regexp? If the : separator is inserted before the regex check, the following should suffice: '/^(0?[1-9]|1[12]):([0-5][0-9])$/' Test script: ?php $testvalues=array(1:00,2:30,12:50,11:00, 13:00,1:69, 01:00,12:59pm,00:40,00:00,a:00,00); $valid_re = '/^(0?[1-9]|1[12]):([0-5][0-9])$/'; foreach ($testvalues as $time) { if (preg_match($valid_re,$time)) { echo $time passes\n; } else { echo $time fails\n; } } Produces output: php twelvehourtimecheck.php 1:00 passes 2:30 passes 12:50 passes 11:00 passes 13:00 fails 1:69 fails 01:00 passes 12:59pm fails 00:40 fails 00:00 fails a:00 fails 00 fails -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php