CallMeDiz Posted November 16, 2012 Share Posted November 16, 2012 (edited) 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! Edited November 16, 2012 by CallMeDiz Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 (edited) substr_count() Edited November 16, 2012 by Psycho Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
CallMeDiz Posted November 16, 2012 Author Share Posted November 16, 2012 (edited) 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"); Edited November 16, 2012 by CallMeDiz Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 (edited) 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. Edited November 16, 2012 by Psycho 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.