Re: Coding hints?
| From: | Lars Torben Wilson | Date: | Wed, 08 Nov 2000 02:48:54 +0000 |
| Subject: | Re: Coding hints? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24247@lists.php.net to get a copy of this message | ||
Gavster writes:
> Hi Guys,
>
> Sorry to post with something trivial, but I can't seem to find anything on
> this:
>
> I'm modifying some code to recognize a certain filename scheme, but I've
> run across something I've never seen before.
>
> Here's the code:
>
> $arr = file('sb' .(int)$sb. '.src');
>
> The filename it's looking for is in the form sbN.src (where N = an integer).
>
> The $sb variable is an integer passed via the url.
>
> What I don't get is the (int)$sb part - what is the (int) doing there? Is
> this some sort of funky regex? Or is it casting the $sb string into an
> int? I must admit, I'm at a loss . . .
The second one--any time you see parentheses not relating to a
function's argument list, containing a sole word which is the name of
a data type, it's a cast. More information on casting and PHP data
types is available here:
http://www.php.net/manual/language.types.type-juggling.php
In the above, it would appear that the coder was trying to ensure that
the $sb variable contained only integer data. For instance, if $sb was
equal to '23foo', the (int) $sb would result in only the '23' part
being used. Check out PHP's string conversion rules for more
information here:
http://www.php.net/manual/language.types.string.php#language.types.string.conversion
> What I'm ultimately trying to do is to get the code to recognize files
> where $sb is not only a number, but is also a number with a single char
> appended to it such as [a-z]. Can't this be done with a straightforward regex?
>
>
> Any pointers would be greatly appreciated . . . =0)
Sure--but it'll make the whole thing slightly longer:
if (preg_match('/^[0-9]+[a-z]?$/i', $sb)) {
echo "$sb matched<br>\n";
} else {
echo "$sb did not match<br>\n";
}
Step-by-step explanation of the regex:
/ At the beginning and end: needed by preg_match(); anything between
the // marks is part of the regex; anything following it is a modifier
which changes some part of the regex's interpretation.
^ Must match starting from the beginning of the string.
[0-9] Match digits from 0-9...
+ ...one or more times (i.e. must be at least one digit).
[a-z] Next, match chars from a-z...
? ...zero or one times (i.e. there may be at most one char).
$ Must match right up to the end of the string.
/ See above; this is the closing / delimiter.
i Make search case-insensitive.
This is a bit simplified, but there is more information in the manual:
http://www.php.net/manual/ref.pcre.php
Hope this helps,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+