如果缺少 http:// 前缀,请在 URL 中添加
您好,我有一个非常简单的代码
<a href="'.$aProfileInfo['Website'].'" target="_self">
<div class="callButton">Website</div>
</a>
问题是,如果用户不输入 http://,链接将指向我的网站,而不是应有的外部网站。
如何在 PHP 中检查用户是否未输入 http:// 并在不存在时自动添加它?
Hello I have a very simple code
<a href="'.$aProfileInfo['Website'].'" target="_self">
<div class="callButton">Website</div>
</a>
The problem is that if the user does not enter http:// the link will then point to my website and not to the external website as it should.
How do I check in PHP if the user has not entered http:// and automatically add it when it is not there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为你最好使用内置函数
parse_url()
它返回一个关联数组及其组件,如下所示:
I think you'd better use the built in function
parse_url()
which returns an associative array with its componentssomething like this will work for you:
我个人使用这个,部分取自 php 文档
I personally use this, which is partially taken from the php docs
一个简单的解决方案可能不适用于所有情况(即“https://”):
A simple solution which may not work in all cases (i.e. 'https://'):
解决这个问题有两种方法:url解析和正则表达式。
有些人会说 url 解析正确,但正则表达式在这种情况下也同样有效。我喜欢能够为这样的事情使用简单的一行行,特别是因为这在模板文件中很常见,您可能需要在 echo 语句中使用一行行来保持可读性。
正则表达式
我们可以使用 preg_replace 在单个函数调用中完成此操作。
这在字符串的开头使用
负向前视
来查找http://
或https://
。如果找到其中任何一个,则不会进行替换。如果没有找到,它会用http://
替换字符串的开头(0 个字符),本质上是将其添加到字符串前面而不对其进行修改。在上下文中:
URL 解析
其作用是通过
parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)
找出链接上是否存在方案。然后使用三元运算符,如果找到,则输出''
;如果未找到,则输出'http://'
。然后它将链接附加到该链接上。在上下文中:
There are two ways of tackling this problem: url parsing and regular expressions.
Some will say url parsing is right, but regular expressions work just as well in this case. I like being able to have simple one-liners for things like this especially because this would be a common occurrence in template files where you may need a one-liner inside an echo statement to maintain readability.
Regular Expressions
We can do this in a single function call with preg_replace.
This uses a
negative lookahead
at the beginning of the string that looks forhttp://
orhttps://
. If either are found, the replace doesn't happen. If they aren't found, it replaces the beginning of the string (0 characters) withhttp://
essentially prepending this to the string without modifying it.In context:
URL Parsing
What this does is find out if a scheme is present on the link throught
parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)
. Then using a ternary operator, it will either output''
if there was one found or'http://'
if one wasn't found. Then it appends the link onto that.In context:
您可以使用
strpos
:You could use
strpos
:您可以将此函数用作通用函数如果在字符串中找不到数组中的任何内容,则可以向其添加一些内容。
You can use this function as a general if nothing from the array is found in the string append something to it.
您还可以考虑“http(s)”必须位于 url 的开头:
You also may take into account that "http(s)" must be at the beginning of the url:
像这样的东西吗?
Something like this?
这是另一个 字符串减法 的示例:
// ....
Here is another example with string subtraction:
// ....
我相信大卫的回答是正确的方法是这样,但可以这样简化:
I believe David's answer is the proper way to do this, but it can be simplified like this: