-2

I have a string www.waada.com

Why

ltrim("www.waada.com", "www.") returns aada.com

OR

ltrim("www.waada.com", "ww.") returns aada.com

OR

ltrim("www.waada.com", "w.") returns aada.com

I want waada.com as final output.

It should just trim "www." from left side of the word.

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
  • 3
    Then why do you use ltrim()? Have you read the manual? Use str_replace($str, "www.",""); – Andreas Sep 07 '17 at 06:43
  • Because second argument is a __list of symbols__, not a __word__ – u_mulder Sep 07 '17 at 06:43
  • @Andreas `str_replace(www.waadawww.com, "www.", "")` will have incorrect results – Umair Ayub Sep 07 '17 at 06:44
  • https://stackoverflow.com/questions/34005517/php-removing-www-from-url https://stackoverflow.com/questions/6336281/php-remove-www-from-url-inside-a-string – u_mulder Sep 07 '17 at 06:46
  • @Umair well that was not your question. If that is the case the Substr the string so you only replace at the start of the string – Andreas Sep 07 '17 at 06:46
  • Why downvote? its not a stupid question – Umair Ayub Sep 07 '17 at 15:26
  • If you would have read the manual about the function you tried to use it would be obvious. `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.**` – Andreas Sep 07 '17 at 15:38

2 Answers2

2

If the string is as per comment with www at the end then use preg_replace.

Echo preg_replace("/^www\./", "", "www.waadawww.com");

https://3v4l.org/6cGV4

The pattern is:
At start of string match literally www. and replace with nothing

Andreas
  • 23,610
  • 6
  • 30
  • 62
1

Use preg_replace:

<?php

echo preg_replace("/^www\./","","www.waadawww.com");

https://eval.in/856795

If you are allergic you can avoid regex altogether, but I find the above code much cleaner:

<?php

$url = "www.waadawww.com";
echo strpos($url, "www.") === 0 ? substr($url, 4) : $url;

https://eval.in/856813

ntd
  • 7,372
  • 1
  • 27
  • 44