CallMeDiz Posted November 16, 2012 Share Posted November 16, 2012 Hey guys i'm trying to make a function that returns how many times a certain character is in a certain string with the first parameter being a string and the second being a character. for example if i wanted to see how many of the characters o are in halloween, so I could call it like this. $numberOfO = numberofchars( "halloweeno", "o"); // should return 2 Replys would be much appreciated! Link to comment https://forums.phpfreaks.com/topic/270773-cant-figure-out-a-function-that-displays-instances-of-characters/ Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 substr_count() Link to comment https://forums.phpfreaks.com/topic/270773-cant-figure-out-a-function-that-displays-instances-of-characters/#findComment-1392851 Share on other sites More sharing options...
CallMeDiz Posted November 16, 2012 Author Share Posted November 16, 2012 Im trying to learn how to make a simples function called numberofchars that integrates that though.. thanks anyways. Link to comment https://forums.phpfreaks.com/topic/270773-cant-figure-out-a-function-that-displays-instances-of-characters/#findComment-1392852 Share on other sites More sharing options...
CallMeDiz Posted November 16, 2012 Author Share Posted November 16, 2012 functions confuse the hell out of me EDIT: Figured it out, wow I feel dumb. function numberofchars($mystring,$mychar) { print substr_count($mystring, $mychar); } $numberOfO =numberofchars( "halloweeno", "o"); Link to comment https://forums.phpfreaks.com/topic/270773-cant-figure-out-a-function-that-displays-instances-of-characters/#findComment-1392853 Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 functions confuse the hell out of me EDIT: Figured it out, wow I feel dumb. function numberofchars($mystring,$mychar) { print substr_count($mystring, $mychar); } $numberOfO =numberofchars( "halloweeno", "o"); You just created a function that does exactly what an existing function already does. That is pointless. You could just do . . . print substr_count($mystring, $mychar); . . . instead of making a function. The reason to build your own function is to build a repeatable process to do something that doesn't already exist. Link to comment https://forums.phpfreaks.com/topic/270773-cant-figure-out-a-function-that-displays-instances-of-characters/#findComment-1392854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.