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"; Link to comment https://forums.phpfreaks.com/topic/29011-can-u-count-how-many-as-in-a-string/ 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 Link to comment https://forums.phpfreaks.com/topic/29011-can-u-count-how-many-as-in-a-string/#findComment-132914 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 :( Link to comment https://forums.phpfreaks.com/topic/29011-can-u-count-how-many-as-in-a-string/#findComment-132916 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.. Link to comment https://forums.phpfreaks.com/topic/29011-can-u-count-how-many-as-in-a-string/#findComment-132930 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] Link to comment https://forums.phpfreaks.com/topic/29011-can-u-count-how-many-as-in-a-string/#findComment-132937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.