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
<? php $myStr = "HelloWordl"; echo substr($myStr,0,5); ?>
To get the first character from a string, we can use the substr() function by passing 0,1 as second and third arguments in PHP.
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
PHP: substr() function The substr() function used to cut a part of a string from a string, starting at a specified position. The input string. Refers to the position of the string to start cutting. A positive number : Start at the specified position in the string.
$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.
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With