Reputation: 419
need regular expression which could match a string like that "1B7FL26X3WS731388"
. Alphanumeric 17 character lenght.
I am using this expression.
$rEX = '/([A-Z0-9]){17}/';
but it also return a portion from a string like this "FGD798791B7FL26X3WS731388POPOD"
;
I need to select a string which is exactly 17 character long 18th character should not be Alphanumeric.
Upvotes: 0
Views: 368
Reputation: 736
try this....
<?php
$title='1B7FL26X3WS731388';
$result = preg_replace("/[^a-zA-Z0-9]/", "", $title);
echo strlen($result);
?>
Upvotes: -1
Reputation: 961
You should use ^ $ delimiters
$rEX = '/^([A-Z0-9]){17}$/';
To only allow uppercase alphanumeric 17 length string
You regular expression will allow all strings that contains a SUBSTRING of uppercased alphanumeric 17 lenght string.
Upvotes: 1
Reputation: 46675
$rEX = '/[^A-Z0-9]([A-Z0-9]){17}[^A-Z0-9]/';
should do. [^...]
negates the character class.
Upvotes: 0