umarsa Posted February 16, 2009 Share Posted February 16, 2009 Hello again.. I wanted to know if there is anyway that you can acount how many times a phraze is found in a string, for example: $string = "Hello how are you today are you feeling well?"; Now i want to know how many times the word "are" appears in the string, is there a way i can make a php script to do this? 'are' appears "2" times, so is there a way? Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/ Share on other sites More sharing options...
Brian W Posted February 16, 2009 Share Posted February 16, 2009 preg_match() http://us.php.net/manual/en/function.preg-match.php Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763456 Share on other sites More sharing options...
Mark Baker Posted February 16, 2009 Share Posted February 16, 2009 substr_count() Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763458 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 But can you show me exactly how i would use those? like a example script Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763459 Share on other sites More sharing options...
Brian W Posted February 16, 2009 Share Posted February 16, 2009 excuse me, it is actually preg_match_all() that you want http://us.php.net/manual/en/function.preg-match-all.php preg_match will only return 0 for not found or 1 for found... preg_match_all will count the occurrences. Mark also supplied a valid solution. If you want to use preg_match_all() with your example, this is how. <?php $string = "Hello how are you today are you feeling well?"; echo preg_match_all("/are/", $string, $matches); ?> Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763461 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 You guys rock! A reply within 5 mins and y problem solved within 10 !! thanks so much Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763468 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 Okay.. I went back to try the script and when i try to search my phraze "[img]" i get 27 results... There is only one of them in my script.. So went and explored a bit and made this: "/[[img]]/" As the search thing but now that counts "[img=http://" AND "]" how do i fix this? Link to comment https://forums.phpfreaks.com/topic/145434-solved-count-amount-of-time-a-phraze-appears-in-a-string/#findComment-763547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.