Reputation: 724
I am running the following code in PHP. My intention is to get "contact.html" in the response, but what I actually get in the output is ntact.html
$str = 'http://localhost/contact.html';
echo $str . "<br>";
echo ltrim($str,'http://localhost');
Any thoughts why PHP is behaving this way and what can I do to fix this?
Upvotes: 4
Views: 1308
Reputation: 4302
ltrim works up there is no match in your character_mask
which in your case http://localhost
.
The output will be like this ntact.html
why ?
It will match http://localhost
and after that there is /
it will remove it because it is in character mask and so on.
why stopped at n
because it is not in your charter mask.
So , ltrim will continue remove unless there is no match in character mask
$str = 'http://localhost/contact.html';
echo ltrim($str, 'http');// output ://localhost/contact.html
and here i will add only one in mask /
and it will remove both //
$str = 'http://localhost/contact.html';
echo ltrim($str, 'http:/');// output localhost/contact.html
Upvotes: 1
Reputation: 41820
Other answers explain why ltrim
isn't doing what you thought it would, but there's probably a better tool for this job.
Your string is a URL. PHP has a built-in function to handle those neatly.
echo parse_url($str, PHP_URL_PATH);
(parse_url
does return the path with a leading slash. If you need to remove that, then ltrim
will work just fine since you'll only trimming one character.)
Upvotes: 3
Reputation: 476
ltrim
doesn't do what you think it does.
It uses a character collection, so all characters within are deleted.
You should delete the substring using str_replace
.
http://php.net/manual/en/function.str-replace.php
$str = 'http://localhost/contact.html';
echo $str . "<br>";
echo str_replace('http://localhost/', '', $str);
Output:
http://localhost/contact.html
contact.html
I do realize that you're trying to only replace a string that's at the beginning of your string, but if you have an http://localhost later in your string, you might have bigger problems.
Documentation on ltrim: http://php.net/manual/en/function.ltrim.php (The Hello World example should be enlightening on explaining exactly what ltrim is doing)
Another example of ltrim misuse: PHP ltrim behavior with character list
Upvotes: 6
Reputation: 26490
From the manual on ltrim()
(emphasis mine):
You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With
..
you can specify a range of characters.
That means that you list a set of characters to be removed, not a word/string. Here's an example.
$str = "foo";
echo ltrim($str, "for"); // Removes everything, because it encounters an F, then two O, outputs ""
echo ltrim($str, "f"); // Removes F only, outputs "oo"
echo ltrim($str, "o"); // Removes nothing, outputs "foo"
That means that any character listed in the character mask would be removed. Instead, you can remove the beginning of the string by str_replace()
, by replacing http://localhost
with an empty string.
$str = 'http://localhost/contact.html';
echo $str . "<br>";
echo str_replace('http://localhost', '', $str);
Upvotes: 2
Reputation: 8515
As far as I know ltrim()
is used for stripping whitespaces from the beginning of a string. See documentation.
If you want to take the string after http://localhost/
you can use substr():
$str = 'http://localhost/contact.html';
echo $str . "<br>";
echo substr($str,18); // 18 is the length + 1 of http://localhost/
Upvotes: 0