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.
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).
Do not use regex. There are simpler solutions available.
Try this:
$theString = 'www.stackoverflow.com';
$theName = explode('.', $theString)[1];