Removing www. and .com from string using regex [closed]

青春壹個敷衍的年華 提交于 2019-12-14 03:32:31

问题


I want to remove 'www.' and '.com' from this string 'www.example.com'

I want to do it in one step with regex but I am still learn regex. Any help and explanation of the flags and syntax will be appreciated so I can get an understanding of what it is doing.


回答1:


echo preg_replace('#^www\.|\.com$#', '', 'www.example.com');

Start from here: http://rubular.com/r/MvyPO9ijnQ

But you really shouldn't be doing that with regexp, and especially not in one one fall-swoop whatever it means.

Take a look at the: http://php.net/manual/en/function.parse-url.php (I know you want to learn regexp by parsing URL, but don't).




回答2:


Do not use regex. There are simpler solutions available.

Try this:

$theString = 'www.stackoverflow.com';
$theName = explode('.', $theString)[1];


来源:https://stackoverflow.com/questions/11978584/removing-www-and-com-from-string-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!