Jump to content

any number except 7[0-9]{4}


michaellunsford

Recommended Posts

I'm wanting to capture [0-9]{0,6} but [b]exclude[/b] the zip code (7[0-9]{4}) from the result.

example 1 "78876 555 easy street" should return "555"
example 2 "1095012 LBJ Highway, 71231" should return "1095012"
example 3 "7000 East Main Street" should return "7000"

Just can't figure it out.
Link to comment
https://forums.phpfreaks.com/topic/34515-any-number-except-70-94/
Share on other sites

See if this works for the rest of your dataset. I changed the max to 7 since you said "1095012" should match--it is 7 numbers, not 6.

[code]
<pre>
<?php
$tests = array(
"78876 555 easy street", ### should return "555"
"1095012 LBJ Highway, 71231", ### should return "1095012"
"7000 East Main Street" ### should return "7000"
);
foreach ($tests as $test) {
preg_match('/\b(?!7\d{4})\d{1,7}\b/', $test, $matches);
print_r($matches);
}
?>
</pre>

[/code]
[quote author=effigy link=topic=122751.msg506671#msg506671 date=1169011514]I changed the max to 7 since you said "1095012" should match--it is 7 numbers, not 6.
[/quote]

odd, my PHP reads it like this [1]{6}=7

Great it works! Now, what's the difference between [0-9] and \d? preference, or is it more than that?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.