SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast komrad's Avatar
    Join Date
    Mar 2007
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to count how many characters in a paragraph?

    I want to be able to count how many characters are in a paragraph excluding whitespace. Anybody help me?

  2. #2
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go :

    PHP Code:
     $para = <<<EDT
    This is just a test paragraph.
    Second line in the test paragraph.
    Third line in the test paragraph.
    EDT;
     
     
    $new_para preg_replace('/\s+/',''$para);
     
     echo 
    strlen($new_para); 

  4. #4
    SitePoint Enthusiast komrad's Avatar
    Join Date
    Mar 2007
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dman_2007 View Post
    Here you go :

    PHP Code:
     $para = <<<EDT
    This is just a test paragraph.
    Second line in the test paragraph.
    Third line in the test paragraph.
    EDT;
     
     
    $new_para preg_replace('/\s+/',''$para);
     
     echo 
    strlen($new_para); 
    Is whitespace counted in this code? Ok, would try it.

  5. #5
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by komrad View Post
    Is whitespace counted in this code? Ok, would try it.
    No. This line removes all whitespace from the paragraph.
    PHP Code:
    $new_para preg_replace('/\s+/',''$para); 

  6. #6
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to keep most of the whitespace (spaces newlines etc), you can use trim($var) to get rid of whitespace only at the start and end of the string.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •