Page 1 of 1

String issue

Posted: Fri Nov 12, 2004 5:14 am
by mjseaden
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

Posted: Fri Nov 12, 2004 6:18 am
by kettle_drum
1) Regex
2) Regex again or substr().

Posted: Fri Nov 12, 2004 6:23 am
by mjseaden
Any idea what regex?

Cheers

Mark

Posted: Fri Nov 12, 2004 6:37 am
by kettle_drum
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

Posted: Sat Nov 13, 2004 12:24 am
by jl
Use this to test the regex and see what's returned in $matches

Code: Select all

$str='magXXr_tot_ext' ;
$pat='/^mag(&#1111;a-zA-Z]&#123;2&#125;)r_tot_ext$/' ;

preg_match($pat, $str, $matches) ;

print_r ($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.