Jump to content
MakeWebGames

... Limit String Length


Recommended Posts

Basically, this function will limit a strings length. If the string length is above the $limit, it is stripped down to size, and 3 dots (...) are added to the end of the string. If it isnt, the original string is returned. This could be useful for previewing text before clicking "Read More" or etc.

 


 function limit_str($str,$limit=0){
    if(strlen($str) > $limit){
      $new_str = strip_tags(substr($str,0,$limit).'...');
    } else {
      $new_str = $str;
    }
 return $new_str;
}

/* 
echo limit_str("Hello World",6); // Returns - Hello W...
echo limit_str("Hello"); // Returns Hello

/*

Link to comment
Share on other sites

echo limit_str("Bla", 5);

Returns <a hre... and then your page is broken depending on the browser because a tag is left unclosed.

I would suggest using strip_tags first, then never cut off a string in the middle of a word, only at the start/end of a word.

Link to comment
Share on other sites

You mean substr? strlen just checks the length

I think the meaning is that if you do not strip tags before taking into account the string length, it will skew your results. Say if you wanted to limit the length of

 

[url="/something/here/in/the/url"]this is something[/url]

 

to a limit of 12, then it will return <a href="/so but if you do it after strip tag, then it will be this is some.

However, if you want to preserve tags, a better method would be to use regular expressions (i.e. the preg functions) to match tags, store them in a separate variable then merge them after limiting your string.

 

function limit_str($str, $limit=0)
{
if (preg_match_all('/<[a-zA-Z0-9="]>(.*)<[\/a-zA-Z0-9]>/', $str, $match, PREG_PATTERN_ORDER))
{
 $new_str = '';
 $i = 0;
// combines all the non-tag matches into one big string
 foreach ($match[1] as $mod_str)
 {
$new_str .= $mod_str[$i];
$i++;
 }
}
if(strlen($str) > $limit)
{
   $new_str = substr($str,0,$limit).' ...';
} 
else 
{
   $new_str = $str;
}
if ($match[0])
{
 $new_str = preg_replace('/<[^\/a-zA-Z0-9="]>/', $new_str, $match[0]);
}
return $new_str;
}

 

Please note this modified version of the function is incredibly rough (the regex is horrible, lol) and not tested or anything, but hopefully it is a good starting point. LOL, this is really quite awful, especially the last part. Perhaps using preg_match and checking against the number of matches it makes might be a better option.

Link to comment
Share on other sites

I realized the example I gave above was really bad. It was written with a four year-old tugging on my leg most of the time. So when the kiddo went down for a nap, I took a few minutes and went back and actually tested it and modified it.

So while it is still quite crude, at least it mostly functions

 

function limit_str ($str, $limit)
{
if (preg_match_all('/<[^>]+>(.*)<\/[^>]+>/', $str, $match, PREG_PATTERN_ORDER))
{
	strlen($match['1']['0']) > $limit
		? $lim_str = substr($match['1']['0'], 0, $limit).' ...'
		: $lim_str = $match['1']['0'];
}
else
{
	$lim_str = $str;
}
if ($lim_str)
{
	$newer_str = preg_replace('/(<[^>]*>)(.*?)(<\/[^>]*>)/is','$1'.$lim_str.'$3', $str);
}
return $newer_str;
}

 

This works for something like

 

[i]HI[/i] or [url="#"]HI[/url]

 

but it will break with an unclosed tag like img or hr. And it will not match nested tags. So you can see it requires a lot more work to really be effective for general use.

Link to comment
Share on other sites

I realized the example I gave above was really bad. It was written with a four year-old tugging on my leg most of the time. So when the kiddo went down for a nap, I took a few minutes and went back and actually tested it and modified it.

So while it is still quite crude, at least it mostly functions

 

function limit_str ($str, $limit)
{
if (preg_match_all('/<[^>]+>(.*)<\/[^>]+>/', $str, $match, PREG_PATTERN_ORDER))
{
	strlen($match['1']['0']) > $limit
		? $lim_str = substr($match['1']['0'], 0, $limit).' ...'
		: $lim_str = $match['1']['0'];
}
else
{
	$lim_str = $str;
}
if ($lim_str)
{
	$newer_str = preg_replace('/(<[^>]*>)(.*?)(<\/[^>]*>)/is','$1'.$lim_str.'$3', $str);
}
return $newer_str;
}

 

This works for something like

 

[i]HI[/i] or [url="/index.php?form=PostAdd&threadID=32976#"]HI[/url]

 

but it will break with an unclosed tag like img or hr. And it will not match nested tags. So you can see it requires a lot more work to really be effective for general use.

Well anyone feel free to do w/e you want with it. :)

Link to comment
Share on other sites

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.   Paste as plain text instead

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