tmyonline Posted March 6, 2008 Share Posted March 6, 2008 Guys: Is there a function that I can check if a string contains an alphabet ? Thanks. Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/ Share on other sites More sharing options...
uniflare Posted March 6, 2008 Share Posted March 6, 2008 do you mean an entire alphabet a through to z, or just contains only alphabet characters? use preg_match("/^[a-zA-Z]+$/i",$string); this function will retru n1 on success or 0 on failure. For help with RegEx use the regex forum. hope this helps, Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485162 Share on other sites More sharing options...
tmyonline Posted March 6, 2008 Author Share Posted March 6, 2008 Hi uniflare, I just need to know if my string contains one alphabet out of the alphabetical character set [a-z] case insensitive. I tried your "preg_match()" and it returned 0. So, did it tell me that my string did not contain any alphabet ? Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485172 Share on other sites More sharing options...
cooldude832 Posted March 6, 2008 Share Posted March 6, 2008 the ^ in front says if the []regex string doesn't have the stuff in brackets return 1 returning 0 would suggest that it fails the preg_match meaning it does have characters Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485177 Share on other sites More sharing options...
uniflare Posted March 6, 2008 Share Posted March 6, 2008 erm, correct me if im wrong but the ^ means "Start of String", so basically match 1 or more ( + ) "A-Z" characters from the start to the end ( $ ) of the string, insensitively (upper & lower case cahracters, i). basically the entire string must consist of a-z characters to match. returning 0 = contains invalid characters returning 1 = string matched, only contains a-z hope this helps, Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485192 Share on other sites More sharing options...
tmyonline Posted March 6, 2008 Author Share Posted March 6, 2008 uniflare, the carot sign "^" means the start of a string if this were in Apache. In regular expression, as I remember, "^" means the negation of what inside the square bracket [ ]. Anyway, I tried your regular expression with many different strings and they all returns 0. So,... something is not working! Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485224 Share on other sites More sharing options...
uniflare Posted March 6, 2008 Share Posted March 6, 2008 strange it works for me: <?php $string = $_GET['string']; $res = preg_match("/^[a-zA-Z]+$/i",$string); print_r($res); ?> though im using apache and the carot works for me, is there another way to use a start of string symbol? like the carot symbol, otherwise it would return 1 as long as the last few character were alphabetic. ----- for a botch job (temp fix), you could try: <?php $string = $_GET['string']; if(count(preg_match_all("/[a-z]+/i",$string,$matches)) == 1){ echo("Username: ".$matches[0][0]." is valid"); }else{ echo("username invalid"); } ?> preg_match_all is slightly different to preg_match, it can return higher than 1 (for more than 1 matches), but i assume is slower (as it counts the matches) hope this helps, Link to comment https://forums.phpfreaks.com/topic/94753-need-to-check-if-a-string-contains-an-alphabet/#findComment-485235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.