An example:
<?php
$originalstr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et nisl in diam fermentum convallis et sit amet nulla. In at massa ipsum. Vestibulum tempus egestas elit, sollicitudin ullamcorper mauris dictum in. Etiam non mollis velit. Vestibulum tincidunt urna ut posuere placerat. Ut cursus ipsum non ipsum auctor, vitae accumsan sem pretium. Praesent porta vestibulum nisl, a malesuada nisi finibus at. Morbi aliquet vitae ante et porttitor. Donec laoreet nisi ut neque luctus vulputate. Donec sollicitudin massa at turpis lobortis laoreet. Etiam porta lacinia placerat. Aenean venenatis dui quis elit facilisis porta. Donec vel semper odio. Vivamus mauris neque, egestas ac mi ac, rutrum dignissim dui. Fusce consectetur eleifend luctus. Vestibulum accumsan rutrum magna iaculis tempus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget arcu sodales, porttitor magna nec, scelerisque purus."; // the string
$substr = substr($originalstr, 0, 50);
echo $substr. "<br><hr><br>". $originalstr; //prints the first 50 characters; and then prints the actual string
?>
Output:
Lorem ipsum dolor sit amet, consectetur adipiscing
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et nisl in diam fermentum convallis et sit amet nulla. In at massa ipsum. Vestibulum tempus egestas elit, sollicitudin ullamcorper mauris dictum in. Etiam non mollis velit. Vestibulum tincidunt urna ut posuere placerat. Ut cursus ipsum non ipsum auctor, vitae accumsan sem pretium. Praesent porta vestibulum nisl, a malesuada nisi finibus at. Morbi aliquet vitae ante et porttitor. Donec laoreet nisi ut neque luctus vulputate. Donec sollicitudin massa at turpis lobortis laoreet. Etiam porta lacinia placerat. Aenean venenatis dui quis elit facilisis porta. Donec vel semper odio. Vivamus mauris neque, egestas ac mi ac, rutrum dignissim dui. Fusce consectetur eleifend luctus. Vestibulum accumsan rutrum magna iaculis tempus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget arcu sodales, porttitor magna nec, scelerisque purus.
In the PHP Manual:
substr
(PHP 4, PHP 5, PHP 7, PHP 8)
substr — Return part of a string
Description ¶
substr ( string $string , int $offset , int|null $length = null ) : string
Returns the portion of string specified by the offset and length parameters.
Parameters ¶
string
The input string.
offset
If offset is non-negative, the returned string will start at the offset'th >position in string, counting from zero. For instance, in the string 'abcdef', the >character at position 0 is 'a', the character at position 2 is 'c', and so forth.
If offset is negative, the returned string will start at the offset'th >character from the end of string.
If string is less than offset characters long, false will be returned.
Example #1 Using a negative offset
<?php
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>
length
If length is given and is positive, the string returned will contain at most >length characters beginning from offset (depending on the length of string).
If length is given and is negative, then that many characters will be omitted >from the end of string (after the start position has been calculated when a >offset is negative). If offset denotes the position of this truncation or beyond, >false will be returned.
If length is given and is 0, false or null, an empty string will be returned.
If length is omitted, the substring starting from offset until the end of the >string will be returned.
Example #2 Using a negative length
<?php
$rest = substr("abcdef", 0, -1); // returns "abcde"
$rest = substr("abcdef", 2, -1); // returns "cde"
$rest = substr("abcdef", 4, -4); // returns false
$rest = substr("abcdef", -3, -1); // returns "de"
?>
Return Values ¶
Returns the extracted part of string; or false on failure, or an empty >string.
I do not think they have mentioned anything about memory at all. So, you would need to test the memory usage spike when running the PHP app.
They have mentioned:
If length is given and is positive, the string returned will contain at most >length characters beginning from offset (depending on the length of string).
But due to common sense, they have probably designed it to only take the first number of characters from that variable, and then just stop doing it when the $pos
is reached.
Have fun cod3ing!