-1

I want to trim a String. Here is my code:

$base2 = chop($base,"www.");

$base contains www.example.com I want it to remove the www.

I thought that this would work. I think I have the latest PHP version.

BenM
  • 4,056
  • 3
  • 24
  • 26
Neb
  • 19
  • 1

3 Answers3

2

Use str_replace():

$base2 = str_replace('www.', '', $base);

This replaces www. with nothing, so it gets deleted.

KittMedia
  • 7,368
  • 13
  • 34
  • 38
2

chop is an alias for rtrim, i.e. it only removes characters from the end of the string (so e.g. chop($base,".com") would return "www.example").

Use ltrim instead:

$base2 = ltrim($base,"www.");
Marvin
  • 13,325
  • 3
  • 51
  • 57
0

chop: http://php.net/manual/de/function.chop.php

I don't like replacing anywhere inside the string because you are asking to remove first characters only:

if (stripos($base,'www.') === 0)
{
   $base = substr($base, 4);
}

stripos to solve lower/upper cases.

tomtaylor
  • 56
  • 2