【问题标题】:using URL's names to create css links [closed]使用 URL 的名称创建 CSS 链接 [关闭]
【发布时间】:2013-09-16 17:02:46
【问题描述】:

我正在尝试获取 URL 域名并使用它们来定义 HTML 文件中 css 链接的唯一名称。 例如 - 网站:http://www.nytimes.com/ 使用 'rawurldecode' (PHP) 编码到 css 链接:http%3A%2F%2Fwww.nytimes.com%2F0.css。

注意 - 为了使用链接,必须使用编码(如果我使用“www.nytimes.com.css”,那应该不起作用..)。问题是浏览器无法识别它们。

当您查看插入到 HTML 文件中的输出时,您会看到:

<HTML><HEAD>
<link rel="stylesheet" type="text/css" href="localhost/colors/sitesDownloaded/http%3A%2F%2Fwww.nytimes.com%2F0.css">
<link rel="stylesheet" type="text/css" media="print" href="localhost/colors/sitesDownloaded/http%3A%2F%2Fwww.nytimes.com%2F1.css">
<link href='localhost/colors/sitesDownloaded/http%3A%2F%2Fwww.nytimes.com%2F2.css' type='text/css' rel='stylesheet'/>
<link rel="stylesheet" type="text/css" href="localhost/colors/sitesDownloaded/http%3A%2F%2Fwww.nytimes.com%2F3.css">
<link rel="stylesheet" href="localhost/colors/sitesDownloaded/http%3A%2F%2Fwww.nytimes.com%2F4.css" />
</HEAD></HTML> 

但正如我所说,浏览器无法识别链接。我猜这与编码和字符的使用有关,例如“%”和点。 我试过“rawurldecode”和“urldecode”(PHP),但它们都不起作用..

另一个重要提示: 使用其他唯一名称很容易,但域名必须是 URL 的名称。 有什么想法吗?

【问题讨论】:

  • 错误。为什么它应该起作用? localhost/colors/sitesDownloaded/http://site.com/ 不是有效的 href 位置,它不起作用。
  • 你为什么要这样做?你能去掉http://吗?
  • Amal - 请注意,链接不被称为编码的“http://”原因。它将是包含“http”的字符串.. putvande - 不,因为我将 css 文件保存在本地目录中,我需要它独立于网络。
  • 为什么不直接将主机域回显到文件名中?

标签: php html css


【解决方案1】:

链接 (url) 在字符串末尾包含字符“/”以及“www”。和 "http://", 所有这些都替换为“str_replace”函数:

function cutUrlHttpAndWWW($text){
$filtered_words = array(
    'http://',
    'www.',
    '/',
);

$zap = '';

$filtered_text = str_replace($filtered_words, $zap, $text);

return $filtered_text;
}

现在它正在工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2013-01-17
    • 2014-12-12
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多