0

I am echoing a users first name in the header. I have a test first name with many characters. For eg:

$firstname = "asdfasdfasdfjkljldkafjsddfjakdsjflaksjdfl"

I'm doing

 <?php  $first_name = character_limiter($first_name, 10);  
echo $firstname;?>

But It's not working. It displays all the characters (asdfasdfasdfjkljldkafjsddfjakdsjflaksjdfl) instead of only first 10 characters.

I only want to see teh first 10 characeters. How can I fix this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Taylor
  • 2,981
  • 2
  • 29
  • 70
  • For reference: http://cxzcxz.com/xref/codeigniter-1.7.1/nav.html?_functions/index.html - So, just use [`substr`](http://php.net/substr) if that's what you actually want. – mario Feb 23 '15 at 02:27

4 Answers4

2

Use $firstname as first parameter instead of $first_name (which initially contains nothing) while using character_limiter() function

$firstname = "asdfasdfasdfjkljldkafjsddfjakdsjflaksjdfl";
$first_name = character_limiter($firstname, 10); // limits $firstname to 10 chars and outputs to $first_name
echo $first_name;

In case that was a typo error while you posted your question, then problem might be with loading of Text Helper in Codeigniter which is required for using this function (CI Text Helper).

You can load Text Helper by specifying it in application/config/autoload.php like this:

$autoload['helper'] = array('text');

Or by loading it specifically in a Controller function like this:

$this->load->helper('text');

UPDATE :

Codeigniter maintains "the integrity of words so the character count may be slightly more or less then what you specify" (ref. CI Text Helper)

Let me explain you this by an example:

$firstname = "asdfasdfasdfjkljldkafjsddfjakdsjflaksjdfl"; //contains only single word
$first_name = character_limiter($firstname, 10);

Here, CI tries to limit $firstname to 10 chars but since, it encounters a word, it will not try to break it instead it outputs till the end of the word.

Now say you had used,

$firstname = "asdfa sdfasdfj kljldkafjsddfjakdsjflaksjdfl sdjbfsdufb";

This contains three words, so the output will be asdfa sdfasdfj…

Note that here too the limited string contains more than 10 chars but since CI tries to maintain word integrity, it does not break the last word.

If you need to strictly limit input string by character, then, you'll have to use the inbuilt php function substr() as described by Hudixt.

Deepak
  • 2,487
  • 3
  • 21
  • 27
  • Hi, it was a typo and It still doesn't work because I had the text helper autoloaded. :( – Taylor Feb 23 '15 at 12:46
  • Thank you so much. I have done the "substr()" method so it looks like this currently: ``. How do I add ellipses after the 5 characters? – Taylor Feb 23 '15 at 22:09
  • You're most welcome! You can add an ellipsis using html code for it manually at the end like this : `$first_name = $first_name.'…';` or you can follow any similar questions asked here : [Inserting ellipsis](http://stackoverflow.com/questions/536814/insert-ellipsis-into-html-tag-if-content-too-wide) , [Truncating long strings](http://stackoverflow.com/questions/802175/truncating-long-strings-with-css-feasible-yet/22811590) – Deepak Feb 24 '15 at 04:25
  • There's another function: "ellipsize()" that may be much better. – Tomas Gonzalez Sep 25 '15 at 22:00
1

Yes character_limiter() function does not work for longer word to prevent word break and/or distort meaning. According to codeigniter documentation, it will not try to break long word to maintain its integrity. As per documentation of character_limiter

Truncates a string to the number of characters specified. It maintains the integrity of words so the character count may be slightly more or less than what you specify.

but codeigniter does not stops you from limiting exact character, ahead it mentions in note

If you need to truncate to an exact number of characters please see the ellipsize() function below.

so instead of character_limiter() you can use ellipsize() function and it will do exact same.

Hope it help.

Dheeraj Thedijje
  • 1,053
  • 12
  • 19
0

You can also use substr() for this purpose.If you want to add ... after 10 character then take the help of strlen(). Use the code below

    <?php 
if(strlen($first_name)>10){
 $first_name = substr($first_name,0, 10)."...";  
}
else{
 $first_name = substr($first_name,0, 10);  
}
    echo $first_name;
?>

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
0

Try this,it will fix your issue.

 $this->load->helper('text');
    $string = "your text here";
    $string = character_limiter($string, 10);
    echo $string;

Output:your text…

Raham
  • 4,781
  • 3
  • 24
  • 27
  • I do have the text helper and it's not working still. – Taylor Feb 23 '15 at 12:46
  • @TaylorSwift try like this, $this->load->helper('text'); $string = "your text here"; $string = character_limiter($string, 10); echo $string; Output : your text… – Raham Feb 24 '15 at 05:26
  • @TaylorSwift,I edit the above code as well,you may use the above as well. – Raham Feb 24 '15 at 05:29