-->

Removing www. and .com from string using regex [cl

2019-09-30 08:59发布

问题:

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];