![](/img/trans.png)
[英]How to remove https:// from URL and insert http:// instead of it in string in PHP?
[英]How to extract just the URL from string with doesn't contains the “http” or “https” and remove it
我有一个带有多个文本字段的表单。
我想摆脱用户输入中的所有链接(无论是否以http(s)
开头)。
例如:
大家好,请检查我的网站: http : //www.example.com/page.php?id=5 ,这是我的Facebook页面http://facebook.com/home和我的Twitter www.twitter.com/home
我想摆脱所有链接,这是到目前为止我尝试过的:
$userInput = preg_replace("/\S+\.\S+/", "", $userInput);
它可以工作,但也可以删除PHP
单词。
如果用户未在链接后添加空格,也会失败。
如何编辑正则表达式以满足我的要求?
你可以试试
(?:https?://)?\w{3,}\.\S+
在regex101.com上查看演示,尽管这可能需要一些摆弄。
(?:https?://)? # http:// or https://, both are optional \\w{3,} # at least three word characters \\. # a dot \\S+ # not a whitespace, 1+
PHP
:
$new_string = preg_replace('~?:https?://)?\\w{3,}\\.\\S+~', '', $your_string_here);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.