PHP Regex to Remove Http:// from String

PHP Regex to Remove http:// from string

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

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

remove http and https from string in php

This worked for me,

$http_referer = str_replace($removeChar, "", "https://example.com/");

php: Remove URL from string

If you want to remove everything, link and after the link, like via thing in your example, the below may help you:

$string = "The Third Culture: The Frontline of Global Thinkinghttp://is.gd/qFioda;via @edge";
$regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?).*$)@";
echo preg_replace($regex, ' ', $string);

If you want to keep them:

$string = "The Third Culture: The Frontline of Global Thinkinghttp://is.gd/qFioda;via @edge";
$regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";
echo preg_replace($regex, ' ', $string);

PHP: Remove http://, http://www, https://, https:// from String and get the Domain name and TLD

Try following regex:

(?:^|\s)(?:https?:\/\/)?(?:\w+(?=\.).)?(?<name>.*).(?<tld>(?<=\.)\w+)

See demo at https://regex101.com/r/lI2lB4/2

If you input is

www.google.com
mail.yahoo.com.in
http://microsoft.com
http://www.google.com
http://mail.yahoo.co.uk

Captured content will be:

MATCH 1
name = `google`
tld = `com`

MATCH 2
name = `yahoo.com`
tld = `in`

MATCH 3
name = `microsoft`
tld = `com`

MATCH 4
name = `google`
tld = `com`

MATCH 5
name = `yahoo.co`
tld = `uk`

How to remove 'http://' from a URL in JavaScript

I think it would be better to take into account all possible protocols.

result = url.replace(/(^\w+:|^)\/\//, '');

Regex to remove string inside an URL

preg_replace('#/[^/]*@#', '/', $url);

Breakdown:

  • / matches a slash
  • [^/]* matches a sequence of non-slash characters
  • @ matches the @ character.

So this matches everything from a / to the next @, with no / between them. Then it's replaced with just the slash.

However, it would probably be better to use parse_url() rather than ad hoc parsing with a regexp.

Regex to remove numbers if not in a url

A regex for this looks like

<URL_REGEX>(*SKIP)(*F)|<YOUR_REGEX>

If we agree that each URL starts with http and goes up to the next whitespace or end of string, you can use

preg_replace('/http\S*(*SKIP)(*F)|\s*\+?[0-9][0-9()\-\s+]{4,20}[0-9]/i', '', $story)

See the regex demo.

Here, http\S*(*SKIP)(*F)| matches http and then any zero or more non-whitespace chars, and then the match is failed, the regex engine starts looking for the next match from the failure position. So, the \+?[0-9][0-9()\-\s+]{4,20}[0-9] part will never match in the URLs.



Related Topics



Leave a reply



Submit