hassank1 Posted January 8, 2009 Share Posted January 8, 2009 is there a built in php function to return the occurence of a char in a string? Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/ Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 Try here. Seriously, try actually looking in the manual. Unlike most manuals, it is actually an incredibly useful resource. Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732422 Share on other sites More sharing options...
hassank1 Posted January 8, 2009 Author Share Posted January 8, 2009 I've tried google but I didn't find a build in function. ofcourse I can do my own function but a php one is faster. Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732425 Share on other sites More sharing options...
rhodesa Posted January 8, 2009 Share Posted January 8, 2009 #1) I LOVE http://letmegooglethatforyou.com/ ....it's fantastic the OP asked about occurrences of A character though. to my knowledge, there is no built in function...there are lot's of other ways to do it though. one easy way that comes to mind is to split on it: <?php $str = "Hello World!"; $find = 'o'; $count = count(explode($find,$str)) - 1; print $count; ?> Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732426 Share on other sites More sharing options...
hassank1 Posted January 8, 2009 Author Share Posted January 8, 2009 why the -1 ?? Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732433 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 substr_count would do it. Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732436 Share on other sites More sharing options...
hassank1 Posted January 8, 2009 Author Share Posted January 8, 2009 great .. thx guys Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732437 Share on other sites More sharing options...
rhodesa Posted January 8, 2009 Share Posted January 8, 2009 EXPLODE = SPLIT. If you split on a character, you will get 1 more then you need BEFORE|MIDDLE|AFTER splitting on | will yield 3, when we really need 2 edit: damn....there is a function Link to comment https://forums.phpfreaks.com/topic/139998-solved-number-of-char-in-string/#findComment-732439 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.