Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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: