【问题标题】:Format URL using PHP - http://www.google.com to www.google.com/; Also having trouble with str_replace()使用 PHP 格式化 URL - http://www.google.com 到 www.google.com/; str_replace() 也有问题
【发布时间】:2012-10-18 16:50:01
【问题描述】:

我在执行以下操作时遇到了一些问题..

http://www.google.com --> www.google.com/
https://google.com --> www.google.com/
google.com --> www.google.com/

我正在尝试删除https:// or http://,确保将www. 添加到URL 的开头,然后在URL 不存在的情况下添加尾部斜杠。

感觉我已经弄清楚了其中的大部分,但我无法让 str_replace() 按我的意愿工作。

据我了解,这是str_replace的使用方法:

$string = 'Hello friends';
str_replace('friends', 'enemies', $string);
echo $string;
// outputs 'Hello enemies' on the page

这是我目前所拥有的:

$url = 'http://www.google.com';

echo reformat_url($url);

function reformat_url($url) {
    if ( substr( $url, 0, 7 ) == 'http://' || substr( $url, 0, 8 ) == 'https://' ) { // if http:// or https:// is at the beginning of the url
        $remove = array('http://', 'https://');
        foreach ( $remove as $r ) {
            if ( strpos( $url, $r ) == 0 ) {
                str_replace($r, '', $url); // remove the http:// or https:// -- can't get this to work
            }
        }
    }
    if ( substr( $url, 0, 4 ) != 'www.') { // if www. is not at the beginning of the url
        $url = 'www.' . $url; // prepend www. to the beginning
    }
    if ( substr( $url, -1 ) !== '/' ) { // if trailing slash does not exist
        $url = $url . '/';  // add trailing slash
    }
    return $url; // return the formatted url
}

对于格式化 URL 的方式的任何帮助将不胜感激;我也很好奇我在使用 str_replace 删除 http:// 或 https:// 时做错了什么。如果有人能提供一些关于我做错了什么的见解,我们将不胜感激。

【问题讨论】:

  • 不,这不是你应该使用str_replace的方式。
  • 谢谢 Alvin,我误解了如何使用它。我现在明白它不会修改您提供给它的主题参数。

标签: php url str-replace substr


【解决方案1】:

试试parse_url()

返回值

在严重格式错误的 URL 上,parse_url() 可能会返回 FALSE。

如果省略组件参数,则返回关联数组。至少一个元素将出现在数组中。该数组中的潜在键是:

  • scheme - 例如http
  • host
  • port
  • user
  • pass
  • path
  • query - 在问号之后?
  • fragment - 在井号 # 之后

因此您可以使用此代码访问域:

$url = "https://www.google.com/search...";
$details = parse_url($url);
echo($details['host']);

【讨论】:

    【解决方案2】:

    $url = str_replace($r, '', $url);
    

    而不是

    str_replace($r, '', $url);
    

    因为str_replace 返回一个新字符串;它不会改变$url

    【讨论】:

      【解决方案3】:
      $url = str_replace('http://', '', $url);
      $url = str_replace('https://', '', $url);
      if(substr( $url, 0, 4 ) != 'www.')
      {
          $url = 'www.'.$url;
      }
      $length = strlen($url);
      if($url[$length-1] != '/')
      $url = $url.'/';
      

      【讨论】:

        【解决方案4】:
        public static function formatURLs($t) {
            $t = ' '.$t;
            $t = preg_replace("#([\s]+)([a-z]+?)://([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)#i", "\\1<a href=\"\\2://\\3\" rel=\"nofollow\" target=\"_blank\">\\3</a>", $t);
            $t = preg_replace("#([\s]+)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" rel=\"nofollow\" target=\"_blank\">\\2.\\3\\4</a>", $t);
            $t = preg_replace("#([\s]+)([a-z0-9\-_.]+)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $t);
            $t = substr($t, 1);
            return $t;
        }
        

        我的功能,希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-10
          • 2010-09-21
          • 2018-03-28
          • 2023-03-21
          相关资源
          最近更新 更多