我需要从页面url中切割www。使用php trim()函数。 但是这个函数剪切和首字母,为什么?php修剪(),工作很差
$domain = parse_url('http://wordpresas.com/page/1');
$domain['host'] = trim($domain['host'], 'www.');
pr($domain['host']); //ordpresas.com
我需要从页面url中切割www。使用php trim()函数。 但是这个函数剪切和首字母,为什么?php修剪(),工作很差
$domain = parse_url('http://wordpresas.com/page/1');
$domain['host'] = trim($domain['host'], 'www.');
pr($domain['host']); //ordpresas.com
至于其他已经指出的trim()
第二个参数包含a list of characters这得到修整。
但是,您可以使用preg_replace()
。这将确保只有www.
将被剥离,如果字符串以它开头。
preg_replace('/^www./', '', $domain['host']);
执行此操作的最有效的方法可能是:
if(strncmp('www.', $domain['host'], 4) == 0){
$domain['host'] = substr($domain['host'], 4);
}
它应该有复杂O(1)
:)
修剪()'工作正常。你的代码效果很差 – Leigh 2012-02-03 11:11:40
嘿,有趣。 “trim()”的第二个参数确实是一个*字符列表*,因此该列表中的任何字符都会被截断。没有意识到这一点。 (仍然,这是设计的,你需要一个不同的功能来做你需要的。) – 2012-02-03 11:12:09