如果缺少 http:// 前缀,请在 URL 中添加

发布于 2024-11-13 22:55:43 字数 271 浏览 2 评论 0原文

您好,我有一个非常简单的代码

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

故事未完 2024-11-20 22:55:43

我认为你最好使用内置函数 parse_url() 它返回一个关联数组及其组件,

如下所示:

 if  ( $ret = parse_url($url) ) {

      if ( !isset($ret["scheme"]) )
       {
       $url = "http://{$url}";
       }
}

I think you'd better use the built in function parse_url() which returns an associative array with its components

something like this will work for you:

 if  ( $ret = parse_url($url) ) {

      if ( !isset($ret["scheme"]) )
       {
       $url = "http://{$url}";
       }
}
风尘浪孓 2024-11-20 22:55:43

我个人使用这个,部分取自 php 文档

$scheme = parse_url($link, PHP_URL_SCHEME);
if (empty($scheme))
    $link = 'https://' . ltrim($link, '/');

I personally use this, which is partially taken from the php docs

$scheme = parse_url($link, PHP_URL_SCHEME);
if (empty($scheme))
    $link = 'https://' . ltrim($link, '/');
森林迷了鹿 2024-11-20 22:55:43

一个简单的解决方案可能不适用于所有情况(即“https://”):

if (strpos($aProfileInfo['Website'],'http://') === false){
    $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
}

A simple solution which may not work in all cases (i.e. 'https://'):

if (strpos($aProfileInfo['Website'],'http://') === false){
    $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
}
惟欲睡 2024-11-20 22:55:43

解决这个问题有两种方法:url解析和正则表达式。

有些人会说 url 解析正确,但正则表达式在这种情况下也同样有效。我喜欢能够为这样的事情使用简单的一行行,特别是因为这在模板文件中很常见,您可能需要在 echo 语句中使用一行行来保持可读性。

正则表达式

我们可以使用 preg_replace 在单个函数调用中完成此操作。

preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website'])

这在字符串的开头使用负向前视来查找http://https://。如果找到其中任何一个,则不会进行替换。如果没有找到,它会用 http:// 替换字符串的开头(0 个字符),本质上是将其添加到字符串前面而不对其进行修改。

在上下文中:

<a href="'. preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>

URL 解析

(parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']

其作用是通过 parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) 找出链接上是否存在方案。然后使用三元运算符,如果找到,则输出 '';如果未找到,则输出 'http://'。然后它将链接附加到该链接上。

在上下文中:

<a href="'.((parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>

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.

preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website'])

This uses a negative lookahead at the beginning of the string that looks for http:// or https://. If either are found, the replace doesn't happen. If they aren't found, it replaces the beginning of the string (0 characters) with http:// essentially prepending this to the string without modifying it.

In context:

<a href="'. preg_replace('/^(?!https?:\/\/)/', 'http://', $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>

URL Parsing

(parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']

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:

<a href="'.((parse_url($aProfileInfo['Website'], PHP_URL_SCHEME) ? '' : 'http://') . $aProfileInfo['Website']).'" target="_self">
    <div class="callButton">Website</div>
</a>
∝单色的世界 2024-11-20 22:55:43

您可以使用 strpos

// Trim trailing whitespace
$aProfileInfo['Website'] = trim($aProfileInfo['Website']);

// Test if the string begins with "http://"
if (strpos($aProfileInfo['Website'], 'http://') !== 0) {
  $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
}

You could use strpos:

// Trim trailing whitespace
$aProfileInfo['Website'] = trim($aProfileInfo['Website']);

// Test if the string begins with "http://"
if (strpos($aProfileInfo['Website'], 'http://') !== 0) {
  $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
}
放肆 2024-11-20 22:55:43

您可以将此函数用作通用函数如果在字符串中找不到数组中的任何内容,则可以向其添加一些内容。

function httpify($link, $append = 'http://', $allowed = array('http://', 'https://')){

  $found = false;
  foreach($allowed as $protocol)
    if(strpos($link, $protocol) !== 0)
      $found = true;

  if($found)
    return $link;
  return $append.$link;
}

You can use this function as a general if nothing from the array is found in the string append something to it.

function httpify($link, $append = 'http://', $allowed = array('http://', 'https://')){

  $found = false;
  foreach($allowed as $protocol)
    if(strpos($link, $protocol) !== 0)
      $found = true;

  if($found)
    return $link;
  return $append.$link;
}
妞丶爷亲个 2024-11-20 22:55:43

您还可以考虑“http(s)”必须位于 url 的开头:

if (preg_match('/^https?:\/\//', $aProfileInfo['Website']) === 0) {
    $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
}

You also may take into account that "http(s)" must be at the beginning of the url:

if (preg_match('/^https?:\/\//', $aProfileInfo['Website']) === 0) {
    $aProfileInfo['Website'] = 'http://'.$aProfileInfo['Website'];
}
海风掠过北极光 2024-11-20 22:55:43

像这样的东西吗?

if (!strpos($aProfileInfo['Website'], 'http://')) {
    $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
}

Something like this?

if (!strpos($aProfileInfo['Website'], 'http://')) {
    $aProfileInfo['Website'] = 'http://' . $aProfileInfo['Website'];
}
少女情怀诗 2024-11-20 22:55:43

这是另一个 字符串减法 的示例:

$changeLink = $myRow->site_url;
  if(substr($changeLink, 0, 7) != 'http://') {
     $changeLink = 'http://' . $changeLink;  
}

// ....

echo "<a href=\"" . $changeLink . "\" target=\"_blank\"></a>";

Here is another example with string subtraction:

$changeLink = $myRow->site_url;
  if(substr($changeLink, 0, 7) != 'http://') {
     $changeLink = 'http://' . $changeLink;  
}

// ....

echo "<a href=\"" . $changeLink . "\" target=\"_blank\"></a>";
柠檬色的秋千 2024-11-20 22:55:43

我相信大卫的回答是正确的方法是这样,但可以这样简化:

parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)==''?'http://'.$aProfileInfo['Website']:$aProfileInfo['Website']

I believe David's answer is the proper way to do this, but it can be simplified like this:

parse_url($aProfileInfo['Website'], PHP_URL_SCHEME)==''?'http://'.$aProfileInfo['Website']:$aProfileInfo['Website']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文