Jump to content

Archived

This topic is now archived and is closed to further replies.

abdfahim

[SOLVED] Show first few character of a string

Recommended Posts

I had the following code
[code]$test="This is the string";
echo $test;[/code]
Now I want to echo the first 3 words (or say, first 9 characters) of $test, that is I want to show "This is the" only. How can i do that? Is there any character count and trim method?

Share this post


Link to post
Share on other sites
or
[code=php:0]
list($a, $b, $c) = explode(" ", $test);
echo "$a, $b, $c";
[/code]

Hope this helps

Share this post


Link to post
Share on other sites
To return the first 9 characters:

[code]
$test = "This is the string";

$output = substr($test, 0, 8);

echo $output;
// This is t
?>
[/code]

It doesn't return "he" because PHP counts whitespace as a character.

EDIT: I accidentally pressed Preview so the reply never got sent. Also, you would use 0 and 8, not 9, because 0 - 8 is 9, 0 - 9 is 10.

Share this post


Link to post
Share on other sites

×

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.