Jump to content

[SOLVED] help on truncate(custom) function


watsmyname

Recommended Posts

hello

 

here is my truncate function that truncates if the inputted string is longer than specified characters. If its longer than specified characters, then string is returned with specified length with "..." appended in the last. This function well works on english characters. I want to use it in my language and it wont work properly because one character in our language means 5 characters in english...for example. "ा"  the 7 characters in quote means a single character in my language. So if i want to show 70 characters in English, its fine, but in our language 70 characters means only 10 characters. So is there any work around for this.

 

Here is the function

<?php

function truncate($str, $len, $el = '...')
{
if (strlen($str) > $len)
{
$xl = strlen($el);
 if ($len < $xl) {
	return substr($str, 0, $len);
 }
 $str = substr($str, 0, $len-$xl);
 $spc = strrpos($str, ' ');
 if ($spc > 0) {
	$str = substr($str, 0, $spc);
 }
 return $str.$el;
  }
  return $str;
}

/*
where, $str=input string, $len=length to show, $el=replacing character if in case input string is bigger than specified length.
*/
?>

 

Help much appreciated.

Link to comment
Share on other sites

First you say 5 characters, then you say 7. From your examples I assume you mean seven. Are ALL of the characters in your language going to be 7 characters? If so, just multiple the length by 7. Your function is quite inefficient to begin with. Here is a function I use to do the same thing:

 

function subStringWords($string, $length, $ellipse='...')
{
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

 

A simple modification will have it treat 7 characters as a single character, like so

function subStringWords($string, $length, $ellipse='...')
{
    $length = $length * 7;
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

 

Now, that won't work if all elements are not represented by 7 characters.

Link to comment
Share on other sites

First you say 5 characters, then you say 7. From your examples I assume you mean seven. Are ALL of the characters in your language going to be 7 characters? If so, just multiple the length by 7. Your function is quite inefficient to begin with. Here is a function I use to do the same thing:

 

function subStringWords($string, $length, $ellipse='...')
{
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

 

A simple modification will have it treat 7 characters as a single character, like so

function subStringWords($string, $length, $ellipse='...')
{
    $length = $length * 7;
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

 

Now, that won't work if all elements are not represented by 7 characters.

Thanks a lot that worked fine. How can i determine whether the input string is in my language or in english language, because i want to use same function for both English and Nepali as one row of record might be in English and another might be in my language??

Link to comment
Share on other sites

Use the same function for both, don't run your string through functions like htmlentities() before you pass it to your truncate string, and use the multibyte string functions instead.

No that wont work, length is multiplied by 7 because 7 characters equivalent my language's one character. It wont work for english. I did something, i checked if '&' position is 0 and if ";" position is 6, then surely the string contains my language...

<?php
function truncate($string, $length, $ellipse='...')
{
$posamp=strpos($string,"&");
$possemi=strpos($string,";");
if($posamp==0 && $possemi==6)
{
    	$length = $length * 7;
}
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}
?>

thanks mjdamato and daniel

Link to comment
Share on other sites

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.