lolalola
lolalola

Reputation: 3823

php trim(), work poorly

I need cut from page url www. using php trim() function. But this function cut and first letter, why?

$domain = parse_url('http://wordpresas.com/page/1');
$domain['host'] = trim($domain['host'], 'www.');
pr($domain['host']); //ordpresas.com

Upvotes: 0

Views: 408

Answers (2)

PeeHaa
PeeHaa

Reputation: 72652

As other have stated the second parameter of trim() contains a list of characters which get trimmed.

However you can use preg_replace() for this. This will make sure only www. will be stripped if the string starts with it.

preg_replace('/^www./', '', $domain['host']);

Upvotes: 4

Vyktor
Vyktor

Reputation: 20997

The most effective way to do this is probably:

if( strncmp( 'www.', $domain['host'], 4) == 0){
    $domain['host'] = substr( $domain['host'], 4);
}

It should have complexity O(1) :)

Upvotes: 0

Related Questions