0

I'm looking for a way to count the number of occurrences in a string, I find 2 php function to do this:

  1. substr_count
  2. count_chars

I try to understand what is the difference between this 2 functions.

The major difference i saw is that substr_count is case sensitive, But i didn't understand about count_chars the following statement:

Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.

If anyone can please explain the major differences between this 2 functions, Maybe some example can help.

5
  • Examples for both functions are in the manual, and the description is pretty clear as well IMHO. Commented Mar 8, 2014 at 20:26
  • I try to understand the manual, Can you please be more descriptive. Commented Mar 8, 2014 at 20:27
  • Not unless you explain better what exactly you are having trouble understanding with. Commented Mar 8, 2014 at 20:30
  • Before asking you should read the manual. You only ask a specific question when you need info. Commented Mar 8, 2014 at 20:31
  • You did not, if you did you would have seen the Examples listed there. Not sure how more explaining is possible when there are examples. If you had a hard time understanding the manual, there is no possible answer anyone can give you which will make you understand. Commented Mar 8, 2014 at 20:32

2 Answers 2

3
  • count_chars() counts the number of occurrences of every byte value, whereas substr_count() will count the number of occurrences of a particular substring.

  • substr_count() can be used to search for the number of occurrences of a substring inside a selective region of the string, which is not possible with count_chars().

  • count_chars() returns an array / string depending on the argument, but substr_count() will always return an integer.

Sign up to request clarification or add additional context in comments.

Comments

0

substr_countcounts the occurences of a given string in another string; while count_charscounts the occurences of every single character in the given string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.