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?
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 usingstr_replace
.http://php.net/manual/en/function.str-replace.php
Output:
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