使用php domdocument添加根路径的问题 [英] problem with adding root path using php domdocument

查看:87
本文介绍了使用php domdocument添加根路径的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用php dom文档为那些没有根路径的锚标记添加站点的根路径,直到现在a已经使用str_replace函数创建了一个函数来执行此操作,但是对于某些链接,其添加了3次根路径.然后我应该在此功能中进行编辑.

I would like to add root path of the site for those anchor tag which have not root path using php dom document, Till now a have made a function to do this with str_replace function but for some links its adding three and for times root path. Then what i should to edit in this function.

问题:=问题在于,它为每个锚标签(而不是某些锚标签)添加了三个根路径. $ HTML变量具有许多锚标记,大约有200个以上的链接. 图像也一样.

Problem:= The problem is its adding three and for times root path for every anchor tag, and not for some. $HTML variable has many anchor tags, about above 200 links. And also same for images.

我知道这是一个非常肮脏的问题,但是我错过了什么,我无法得到.

I know that its very dirty question, but what i have missed, i cant getting.

function addRootPathToAnchor($HTML)
{
    $tmpHtml = '';
    $xml = new DOMDocument();
    $xml->validateOnParse = true;
    $xml->loadHTML($HTML);

   foreach ($xml->getElementsByTagName('a') as $a )
   {
      $href = $a->getAttribute('href');
      if(strpos($href,'www' > 0))
        continue;
      else
        $HTML = str_replace($href,"http://www.mysite.com/".$href,$HTML);  

   }

   return $HTML;
}

推荐答案

我在您的代码中看到了一些问题:

I see some problems in your code:

  1. 决定URI是否具有完整的根路径(是完全限定的URI).
  2. 您没有将相对URL解析为基本URL.仅仅追加并不能完成这项工作.
  3. 该函数返回DomDocument对象,而不是字符串.我以为您不想要但我不知道,您还没有写问题.

如何检测网址是否是相对网址.

相对URL未指定协议.因此,我将进行检查以确定href属性是否为完全限定的(绝对)URI(演示):

Relative URLs don't specifiy a protocol. So I would check for that to determine whether or not a href attribute is a fully qualified (absolute) URI or not (Demo):

$isRelative = (bool) !parse_url($url, PHP_URL_SCHEME);

将相对URL解析为基本URL

但是,这不能帮助您正确地将相对URL解析为基本URL.您所做的在概念上是行不通的.在RFC中指定了如何解析相对于基本URL的相对URI( RFC 1808和RFC 3986 ).您可以使用现有的库来为您完成工作,可以使用的是 Net_URL2 :

However this won't help you to properly resolve a relative URL to the base URL. What you do is conceptually broken. It's specified in an RFC how to resolve a relative URI to the base URL (RFC 1808 and RFC 3986). You can use an existing library to just let the work do for you, a working one is Net_URL2:

require_once('Net/URL2.php'); # or configure your autoloader

$baseUrl = 'http://www.example.com/test/images.html';

$hrefRelativeOrAbsolute = '...';

$baseUrl = new Net_URL2($baseUrl);

$urlAbsolute = (string) $baseUrl->resolve($hrefRelativeOrAbsolute);

这篇关于使用php domdocument添加根路径的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆