Jump to content

counting


fishfin

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.