SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: wordwrap

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wordwrap

    Hello,

    I have a string s1. I want to insert newline character for every 100 Maximum characters. The words should not be split.

    example, $s1 = "word1 word2 word3 word4 word5.........wordn";

    if the 100th character is in middle of the word then the newline character should be inserted at the end of the previous ending word.

    suppose 100th character is o in word3 then the newline character should be inserted after word2.

    Can someone help me in this?

    i could have used wordwrap, sometimes the number of characters in the words are smaller and larger in different lines it does not look nice.

    Thanks

  2. #2
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shivam0101 View Post
    Hello,

    I have a string s1. I want to insert newline character for every 100 Maximum characters. The words should not be split.

    example, $s1 = "word1 word2 word3 word4 word5.........wordn";

    if the 100th character is in middle of the word then the newline character should be inserted at the end of the previous ending word.

    suppose 100th character is o in word3 then the newline character should be inserted after word2.

    Can someone help me in this?

    i could have used wordwrap, sometimes the number of characters in the words are smaller and larger in different lines it does not look nice.

    Thanks
    PHP Code:
    function excerptWrap($string$desLength$endChars '...'$before '1'$offChar ' ')
        {
            switch(
    $before)
                {
                    case 
    0:
                    
    $partialString substr($string$desLengthstrlen($string));
                    
    $endPosition strpos($partialString$offChar);
                    return 
    substr($string0$desLength+$endPosition).$endChars;
                    break;
                    
                    case 
    1:
                    
    $string substr($string0$desLength);
                    
    $endPosition strrpos($string$offChar);
                    return 
    $string substr($string0$endPosition).$endChars;
                    break;
                    
                }

        } 
    Run
    PHP Code:
    echo excerptWrap($s1100); 
    It assumes space is the char to cut off, and before means it will cut off before the limit if there's a word in between.


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
  •