小编gUg*_*UgU的帖子

使用php/javascript查找并替换网页中的所有链接

我需要在一些HTML代码的一部分中找到链接,并用两个不同的绝对或基本域替换所有链接,然后是页面上的链接...

我找到了很多想法并尝试了很多不同的解决方案.在这一方面运气不好......请帮助我!谢谢!!

这是我的代码:

<?php
$url = "http://www.oxfordreference.com/views/SEARCH_RESULTS.html?&q=android";
$raw = file_get_contents($url);
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content,'<table class="short_results_summary_table">');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);

echo "{$table}";

$dom = new DOMDocument();
$dom->loadHTML($table);

$dom->strictErrorChecking = FALSE;

// Get all the links
$links = $dom->getElementsByTagName("a");
foreach($links as $link) {
  $href = $link->getAttribute("href");
  echo "{$href}";

  if (strpos("http://oxfordreference.com", $href) == -1) {
  if (strpos("/views/", $href) == -1) {
     $ref = "http://oxfordreference.com/views/"+$href;
  }
  else 
      $ref = "http://oxfordreference.com"+$href;
    $link->setAttribute("href", $ref);
    echo …
Run Code Online (Sandbox Code Playgroud)

php replace href preg-replace

1
推荐指数
1
解决办法
4389
查看次数

标签 统计

href ×1

php ×1

preg-replace ×1

replace ×1