How do you pull first 100 characters of a string in PHP


Question

I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.

Is there a function that can do this easily?

For example:

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";
$string2 = 100charfunction($string1);
print $string2

To get:

I am looking for a way to pull the first 100 characters from a string vari
1
77
11/25/2008 1:43:46 PM

Accepted Answer

$small = substr($big, 0, 100);

For String Manipulation here is a page with a lot of function that might help you in your future work.

175
11/25/2008 1:53:41 PM

You could use substr, I guess:

$string2 = substr($string1, 0, 100);

or mb_substr for multi-byte strings:

$string2 = mb_substr($string1, 0, 100);

You could create a function wich uses this function and appends for instance '...' to indicate that it was shortened. (I guess there's allready a hundred similar replies when this is posted...)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon