String issue

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

String issue

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

1) Regex
2) Regex again or substr().
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Any idea what regex?

Cheers

Mark
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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";
}
?>
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Re: String issue

Post 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.
Post Reply