Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex to Remove http:// from string

Tags:

regex

php

I have full URLs as strings, but I want to remove the http:// at the beginning of the string to display the URL nicely (ex: www.google.com instead of http://www.google.com)

Can someone help?

like image 623
Casey Avatar asked Mar 03 '12 21:03

Casey


People also ask

How to Remove http from string in php?

$removeChar = ["https://", "http://", "/"];

How to Remove http from URL in php?

If you want to remove the only http:// from URL. you have use PHP preg_replace() function. <? php // Remove http:// $input = "http://way2tutorial.com"; $input = preg_replace( "#^[^:/.]

How to Remove http in URL?

To remove http:// or https:// from a url, call the replace() method with the following regular expression - /^https?:\/\// and an empty string as parameters. The replace method will return a new string, where the http:// part is removed. Copied!


1 Answers

$str = 'http://www.google.com'; $str = preg_replace('#^https?://#', '', $str); echo $str; // www.google.com 

That will work for both http:// and https://

like image 169
Sarfraz Avatar answered Sep 23 '22 06:09

Sarfraz