Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

ltrim() Not working as expected

Let's say that I want to remove "This " from "This is some easy text", I would then try to do something like:

$text = "This is some easy text";
$strip = "This ";
$text = ltrim($text, $strip);
echo $text;

You'd think that I'd get "is some easy text" displayed to the console, but instead I get "ome easy text", I know the white space along with "is s" get trimmed because they are in the character_mask parameter but how then would you trim only the word "This " and nothing else, is there any way to do this?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm not a PHP expert, but I whipped this up in a workspace using the strstr function. I think it does what you want it to do.

<?php
$text = "This is some easy text";

$first_word = strstr($text, ' ', true);
echo "$first_word\n";

$without_first_word = strstr($text, ' ');
echo "$without_first_word\n" ;
?>

You can find the documentation on this function here.

Hope this helps! :sparkles: