Could someone please help me with a regular expression (I need it in php and in js) to remove http:// and www. from the beginning of a url string and remove the trailing / if its there.
For Example
http://www.google.com/would begoogle.comhttps://yahoo.com?page=1would beyahoo.com?page=1fancysite.com/articles/2012/would befancysite.com/articles/2012
Heres the code Im using for the JS side:
row.page_href.replace(/^(https?|ftp):\/\//, '')
And heres the code Im using for the php side:
$urlString = rtrim($urlString, '/');
$urlString = preg_replace('~^(?:https?://)?(?:www[.])?~i', '', $urlString);
As you can see the JS regex only removes http:// currently and the php requires two steps to do everything.
wwwto the JS regex? Or why don't you use the the same in both cases? I don't think PHP requires you trim a possible/from the end of the string... that's just how you choose to do it.^(?:https?://)?(?:www[.])?? Looks fine to me, just use it in JS and PHP.