Hi
I have a string of the form
magXXr_tot_ext
Where XX are letters (upper or lower case).
How do
(1) Check if a string is of this form, and
(2) Extract the letters XX from the string?
Many thanks
Mark
String issue
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Umm something along the lines of:
Code: Select all
magXXr_tot_ext
^їa-zA-Z]{6}ї_]{1їa-z]{3}ї_]{1}їa-z]{3}$Code: Select all
<?php
$string = "magXXr_tot_ext";
if(ereg('^[a-zA-Z]{6}[_]{1}[a-z]{3}[_]{1}[a-z]{3}$', $string)){
echo "yes";
}else{
echo "no";
}
?>Re: String issue
Use this to test the regex and see what's returned in $matches
1. If the test sting $str is of the same form as your example (which I assume means everything stays the same except the two XXs) then will be something in $matches, if it's different in any way, $matches will be empty.
2. $matches[1] will contain the part in brackets - i.e. the two letters you want to pull out.
Code: Select all
$str='magXXr_tot_ext' ;
$pat='/^mag(їa-zA-Z]{2})r_tot_ext$/' ;
preg_match($pat, $str, $matches) ;
print_r ($matches) ;2. $matches[1] will contain the part in brackets - i.e. the two letters you want to pull out.