Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/254507-wordcharacter-limiter-help/
Share on other sites

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.

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...

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.