Re: String searching
| From: | Daniel Brown | Date: | Sat, 17 May 2008 06:32:19 +0000 |
| Subject: | Re: String searching | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274464@lists.php.net to get a copy of this message | ||
On Sat, May 17, 2008 at 2:17 AM, Chris W <2wsxdr5@cox.net> wrote:
> I need to find the position of the first character in the string
> (searching from the end) that is not one of the characters in a set. In
> this case the set is [0-9a-zA-z-_]
To find the position of a specific character, RTFM on strpos().
For those not existing in your condition, I'd recommend
everythingbut(), but it's yet to be included in the core. ;-P
> I guess to be even more specific, I want to split a string into to parts
> the first part can contain anything and the second part must be only in
> the set described above.
You can split a string by doing something as simple as this:
<?php
$str = "abcdefghijklmnopqrstuvwxyz";
$d = $str[5]; // $d == position - 1, because count always begins with 0
?>
So to walk backward through the string, while it's not very clean,
you could do:
<?php
$str = "ABCDEF01234567789";
for($i=strlen($str);$i>0;$i--) {
if(preg_match('/[g-z]/i',$str[$i])) {
// Handle your "this is a bad character" condition(s).
// break; /* Or, optionally, continue. */
}
}
?>
Not pretty, but if my mind is still working at 2:30a (EDT), it
should help you out.
--
</Daniel P. Brown>
Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.