iPixel Posted January 6, 2012 Share Posted January 6, 2012 I'm having an issue, and I can't think of a good way to fix it that wont make the code too messy. Long story short, pull data from database and display it. One of the fields is "Description" however on this particular page i want to limit the description view to 250 characters, and i do it like so... if(strlen($row['Description']) > 250) { echo substr($row['Description'],0,250); } This works great, however it has it's issues. This particular one that I need to find a work-around for is if the description contains any HTML, it will break the text following the above echo. I realized this when the 250 limit cut a link in half <a href="http://www.xy and it cuts that off there which then causes my ...more link to assume it's linking to www.xy which clearly returns a broken page. My question is how can i avoid this, is there an easy way to notice wait i just opened a tag, forget 250 limit finish it just before or just after the tag that is being cut off. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 6, 2012 Share Posted January 6, 2012 Is stripping out the HTML tags an option? Quote Link to comment Share on other sites More sharing options...
iPixel Posted January 6, 2012 Author Share Posted January 6, 2012 Is stripping out the HTML tags an option? Unfortunately no. I wish though, that would have made life easy. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted January 6, 2012 Share Posted January 6, 2012 This is a function I use for a similar sort of thing, created it myself a while ago. <?PHP function str_cut($string,$length) { //### Get the string length $strLen = strlen($string); //### If the string length is less than $length, just return the string if($strLen < $length) { return $string; } else { //### Find the next space $nextSpace = stripos($string, ' ', $length); //### If we find the next space, cut the string and add "..." //### If we don't find the next space, return the whole string if(is_int($nextSpace)) { return substr($string,0,$nextSpace).'...'; } else { return substr($string,0,$strLen); } } } $string = 'This is a long message that seems to contain some HTML tags. <a href="http://www.google.co.uk">Google</a>'; echo str_cut($string,25); ?> Try it out, should work nicely. Regards, PaulRyan. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2012 Share Posted January 6, 2012 But if the next space falls within an HTML tag, like between <a and href, the problem still exists. $string = 'This is a long message <a href="[url=http://www.google.co.uk]http://www.google.co.uk[/url]">Google</a>'; echo str_cut($string, 25); The above returns: This is a long message <a... Quote Link to comment Share on other sites More sharing options...
iPixel Posted January 6, 2012 Author Share Posted January 6, 2012 Good catch, however at least that chance is much much much smaller as far as chance of occurring. for now i'm getting a bug with it, breaks my page... trying to figure out why. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted January 6, 2012 Share Posted January 6, 2012 Good point Pikachu2000, I never really used that function for content that contained HTML, just for textual content. Quote Link to comment Share on other sites More sharing options...
laffin Posted January 6, 2012 Share Posted January 6, 2012 You wont find a simple solution. Since you want html tags, this may break the page on any dangling tags such as: <div name="content">blah blah blah</div> If you require the html tags, You may find a solution using strip tags & strpos to find the character limit, than using simplexml and strpos to find the last block within the character limit Quote Link to comment Share on other sites More sharing options...
iPixel Posted January 6, 2012 Author Share Posted January 6, 2012 Yea i know, luckily in my case i really only have to worry about <b> <strong> <a> <i> <u> and that's probably it. So <a> is really the main culprit causing my headache. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted January 6, 2012 Share Posted January 6, 2012 I've slightly modified my code, since it's only <a> you have to worry about, a slightly hacky fix can be added. <?PHP function str_cut($string,$length) { //### Get the string length $strLen = strlen($string); //### If the string length is less than $length, just return the string if($strLen < $length) { return $string; } else { //### Find the next space $nextSpace = stripos($string, ' ', $length); //### Check if we are in a <a> element if($string[$nextSpace-2].$string[$nextSpace-1] == '<a') { $nextSpace = stripos($string, ' ', $nextSpace+1); } //### If we find the next space, cut the string and add "..." //### If we don't find the next space, return the whole string if(is_int($nextSpace)) { return substr($string,0,$nextSpace).'...'; } else { return substr($string,0,$strLen); } } } $string = 'This is a long message <a href="http://www.google.co.uk">Google</a>'; echo str_cut($string,25); ?> Try that out? Regards, PaulRyan. Quote Link to comment Share on other sites More sharing options...
iPixel Posted January 6, 2012 Author Share Posted January 6, 2012 Both versions work perfectly. Thanks very much PaulRyan, your help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2012 Share Posted January 6, 2012 You know, there almost has to be a way to do this with CSS without worrying about the tags. Quote Link to comment 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.