RE: If the first four characters are "0000", then do {}

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Paul M Foster [mailto:paulf@xxxxxxxxxxxxxxxxx] 
> Sent: Monday, January 25, 2010 8:05 PM
> To: php-general@xxxxxxxxxxxxx
> Subject: Re:  If the first four characters are "0000", then do {}
> 
> On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote:
> 
> > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
> >
> > $mydata->restored = "0000-00-00";
> >
> > How do I express this? If the first four characters are 
> "0000", then do {}
> >
> > What I am looking for is in strpos(), no?
> >
> > if (????????????)
> > {
> > }
> 
> >From what I understand, strpos() faster than a lot of other similar
> string functions and much faster than regexps. You could do:
> 
> if (strpos($mydata->restored, '0000') === 0) {
> 	do_stuff();
> }

Ah. Clever use of the "== 0" (tripple equals not necessary).
The OP needs to know if it's the first 4 or not and so position 0 would be
the start. Very nice.

Now, if his data is always of the format XXXX-XX-XX as it was illustrated,
then this will also work

if (strpos($mydata->restored, '0000-') !== false) { do stuff; } 

(note the hyphen) and you should check the === false


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux