Jump to content

Recommended Posts

Could I also do this?

 

$x = substr_count('e', 'I like to eat sheep');

 

Yes.

 

And another thing, how would I tell it to count the number of <enter>'s in the string?

 

\n means a new line, so you can do:

substr_count("\n", $string);

 

Note that it won't work if you use single quotes.

Link to comment
https://forums.phpfreaks.com/topic/110284-counting/#findComment-565879
Share on other sites

And another thing, how would I tell it to count the number of <enter>'s in the string?

 

<?php
$string = "Twas brillig and the slithy toves
	Did gyre and gimble in the wabe
	All mimsy were the borogoves
	And the mome raths outgrabe";
$count = substr_count($string, "\n");                    // NOTE (haystack, needle)

echo $count;                           // --> 3
?>

Link to comment
https://forums.phpfreaks.com/topic/110284-counting/#findComment-565880
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.