PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do we count paragraphs or newlines?


Thanks for your question; there are many possible solutions to this. One is to use the in-built function called substr_count, which counts the number of times that a particular substring appears in the whole string.

For instance if you want to count line breaks - occurrances of the \n character, then this can be done very simply with this function.

Here is an example of how to use substr_count to count newlines:

<?php

$sentence 
"This is a sentence that has lots 

of

line breaks

in it

for no apparent 

reason!!!"
;

$numberoflinebreaks substr_count($sentence,"\n");

echo 
"There are $numberoflinebreaks line breaks in that sentence";
//There are ten line breaks in that sentence
?>



Comment on this Question and Answer >>>

ASK A QUESTION

More strings PHP Questions

How do I find the length of a string in PHP?

How do I break email address into username and hostname?

How do I calculate the metaphone of a string?

how to compare strings in PHP?

How do I find the last occurrence of a character in a string?



Custom Search