Having lurked long enough, it is now time to post my problem
I have a simple input form that posts an article to a MySQL database. 'Returns' in the user input are converted using php's nl2br() function so they are contained in the db as <br /><br />. So far so good.
When I retrieve the posted information, in one use I truncate the content and add a link to 'more' to see the whole article.
$content = substr($myrow[content],0,300);
$content = substr($content,0,strrpos($content,' '));
echo $content;
echo " .... <a href='http://www.htmlforums.com/archive/index.php/full-article.php?artnum=";
echo $myrow[id];
echo "'>more</a><br><br>";
That truncates the content to 300 characters, then strips off any characters from the end back one beyond a space (so as not to show a partial word - which looks dumb). However - and this is the ugly little problem - I just had a posted article where the truncation ended by converting <br /> to <br - exactly what you would expect the code above to do. That turned my link to more into just the word more, no hyperlink.
I guess it could have happened equally if I wasn't stripping characters from the end of $content anyway.
Anyone have a brilliant solution to stop <br /> tags from being split by the truncation?then after the truncation just do a check to see if it left <br and if it did then just add to it or delete it. so basically it coulld be this simple
str_replace("<br","" $contnet);
but it might get the others as well. so it looks like a substr() to check the very last 3 characters and see if they equal the <br part.what abt checkin if $content <br as the last characters and chunkin them off.
ie
I have a simple input form that posts an article to a MySQL database. 'Returns' in the user input are converted using php's nl2br() function so they are contained in the db as <br /><br />. So far so good.
When I retrieve the posted information, in one use I truncate the content and add a link to 'more' to see the whole article.
$content = substr($myrow[content],0,300);
$content = substr($content,0,strrpos($content,' '));
echo $content;
echo " .... <a href='http://www.htmlforums.com/archive/index.php/full-article.php?artnum=";
echo $myrow[id];
echo "'>more</a><br><br>";
That truncates the content to 300 characters, then strips off any characters from the end back one beyond a space (so as not to show a partial word - which looks dumb). However - and this is the ugly little problem - I just had a posted article where the truncation ended by converting <br /> to <br - exactly what you would expect the code above to do. That turned my link to more into just the word more, no hyperlink.
I guess it could have happened equally if I wasn't stripping characters from the end of $content anyway.
Anyone have a brilliant solution to stop <br /> tags from being split by the truncation?then after the truncation just do a check to see if it left <br and if it did then just add to it or delete it. so basically it coulld be this simple
str_replace("<br","" $contnet);
but it might get the others as well. so it looks like a substr() to check the very last 3 characters and see if they equal the <br part.what abt checkin if $content <br as the last characters and chunkin them off.
ie
Code:
$content = substr($myrow[content],0,300);
$content = substr($content,0,strrpos($content,' '));
if (preg_match("/(<br)$/",$content) {
$content = substr($content,0,strrpos($content,'<br'));
}Guess the problem was a little deeper, because the original truncation of the string could have left <, <b, or <br as well as <br .
However, after an RTFM (<!-- m --><a class="postlink" href="http://www.mysql.com/doc/en/String_functions.html#IDX1192">http://www.mysql.com/doc/en/String_func ... ml#IDX1192</a><!-- m -->) session it seems as though this ought to work:$content = substr($myrow[content],0,300);
$content = substring_index($content,'<',1);
That should strip anything left from the originally truncated <br /> regardless of where it was truncated by the 300 character cut.Why not store the original string in the database with newlines (\n)? Then when you want the data, select it from the database and then add the <br>'s with nl2br().