exhaler Posted January 23, 2010 Share Posted January 23, 2010 Hi, i'm trying to figure out how can i do a link break something similar to joomla's text editor. i creating a website that has a news & events section, in that i show the title and part of the text with a read more link. to show part of the text i use this code: $description = substr($row['description'], 0, 150) it is a good method but most of the time the words are chopped in the middle. in the backend i'm using a rich text editor called jQuery WYSIWYG http://code.google.com/p/jwysiwyg/ to add text. how can can show part of the text without having words chopped of at the end?? or make the user choose where the part text should end? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/189564-creating-a-line-break/ Share on other sites More sharing options...
spfoonnewb Posted January 23, 2010 Share Posted January 23, 2010 See wordwrap; http://php.net/manual/en/function.wordwrap.php Quote Link to comment https://forums.phpfreaks.com/topic/189564-creating-a-line-break/#findComment-1000587 Share on other sites More sharing options...
exhaler Posted January 23, 2010 Author Share Posted January 23, 2010 wordwrap doesn't help me since i still need to give it a number. however, i've able to fix my issue by using the <hr> tag the text editor i use creates this tag whenever i press on the horizontal rule: <br><hr size="2" width="100%"> so i use the explode to get all the text before the <hr> tag which the users specified $description = explode("<br><hr size=\"2\" width=\"100%\">", $row['description']); echo $description[0]; now all i have to do is figure how to remove it from the full article page?? i'm thinking about searching for it using preg_match and replacing it with empty quotes Quote Link to comment https://forums.phpfreaks.com/topic/189564-creating-a-line-break/#findComment-1000593 Share on other sites More sharing options...
tryingtolearn Posted January 23, 2010 Share Posted January 23, 2010 have you looked at str_word_count http://php.net/manual/en/function.str-word-count.php Quote Link to comment https://forums.phpfreaks.com/topic/189564-creating-a-line-break/#findComment-1000599 Share on other sites More sharing options...
exhaler Posted January 24, 2010 Author Share Posted January 24, 2010 thanks tryingtolearn, str_word_count didn't help much but a user (charliefrancis at gmail dot com) posted this code at the same page function word_limiter( $text, $limit = 30, $chars = '0123456789' ) { if( strlen( $text ) > $limit ) { $words = str_word_count( $text, 2, $chars ); $words = array_reverse( $words, TRUE ); foreach( $words as $length => $word ) { if( $length + strlen( $word ) >= $limit ) { array_shift( $words ); } else { break; } } $words = array_reverse( $words ); $text = implode( " ", $words ) . '…'; } return $text; } It returns a string with a certain character limit, but still retaining whole words which is perfect for me Quote Link to comment https://forums.phpfreaks.com/topic/189564-creating-a-line-break/#findComment-1000721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.