| 
	
 | 
 Posted by John Dunlop on 10/19/06 14:55 
ollie.mitch@gmail.com: 
 
> I need two ereg expressions in my PHP code. One of them needs to check 
> that a string only contains letters, 
 
'letters' means A-Z? 
 
(untested) 
 
preg_match('/\A[a-z]+\z/i',$subject) 
 
Meaning:  match, starting at the beginning and finishing at the end, 
one or more letters, case insensitive. 
 
> and the other needs to check that the string only contains 
> letters and commas (only one comma at each time). 
 
(untested) 
 
preg_match('/\A(,(?!,)|[a-z]*)+\z/i',$subject) 
 
Meaning:  match, one or more times, a comma that is not followed by 
another comma or match zero or more letters. 
 
> I thought that the code for only containing letters would be: 
> 
> eregi("^([a-z])", $keywords); 
> 
> But this only appears to be checking the first character. 
 
Yeah, you haven't quantified the character class, e.g., /^[a-z]+/, or 
anchored the pattern to the end of the subject, e.g., /^[a-z]+$/. 
 
--  
Jock
 
  
Navigation:
[Reply to this message] 
 |