brown2005 Posted November 30, 2006 Share Posted November 30, 2006 if i have$reply = "Yes I am able to go to the pub tommorrow";can i count how many a's are in it....so $reply_count_a = "2"; Quote Link to comment Share on other sites More sharing options...
craygo Posted November 30, 2006 Share Posted November 30, 2006 Look at the substr_count function in the manual!![code]<?php$letter = "a";$reply = "Yes I am able to go to the pub tommorrow";$number = substr_count($reply, $letter);echo $number; // echos 2?>[/code]Ray Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 30, 2006 Share Posted November 30, 2006 Another way that comes to mind is preg_match_all(), but it may be overkill for what you're after:[code]<?php$String = "Yes I am able to go to the pub tommorrow";preg_match_all('|a|i', $String, $matches, PREG_SET_ORDER);echo count($matches) . " a's found!";?>[/code]Try it and see... I haven't actually tested it yet :( Quote Link to comment Share on other sites More sharing options...
brown2005 Posted November 30, 2006 Author Share Posted November 30, 2006 ok well the first one works perfect, but ill try urs too.. thanks very much to the both of u.. Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 30, 2006 Share Posted November 30, 2006 here´s another way[CODE]<?php$reply = "Yes I am able to go to the pub tommorrow";echo strlen(preg_replace('/[^a]/is', '', $reply));?>[/CODE] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.