添加rel =“nofollow”保存数据的同时

发布于 2024-11-04 12:53:54 字数 277 浏览 0 评论 0原文

我的应用程序允许用户在我的网站上发表评论。它工作正常。我还有工具可以在其中插入他们的网络链接。我对带有自己的链接的内容感到满意。

现在我想将 rel="nofollow" 添加到他们编写的内容的每个链接中。

我想在保存数据时使用 php ie 添加 rel="nofollow" 。

那么使用 php 添加 rel="nofollow" 或更新 rel="someother" 与 rel="someother nofollow" 的简单方法是什么,

一个很好的例子会非常有效

I have my application to allow users to write comments on my website. Its working fine. I also have tool to insert their weblinks in it. I feel good with contents with their own weblinks.

Now i want to add rel="nofollow" to every links on content that they have been written.

I would like to add rel="nofollow" using php i.e while saving data.

So what's a simple method to add rel="nofollow" or updated rel="someother" with rel="someother nofollow" using php

a nice example will be much efficient

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏了南城 2024-11-11 12:53:54

正则表达式确实不是处理 HTML 的最佳工具,尤其是当 PHP 内置了一个非常好的 HTML 解析器时。

如果 rel 属性是,此代码将处理添加 nofollow已经有人居住。

$dom = new DOMDocument;

$dom->loadHTML($str);

$anchors = $dom->getElementsByTagName('a');

foreach($anchors as $anchor) { 
    $rel = array(); 

    if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
       $rel = preg_split('/\s+/', trim($relAtt));
    }

    if (in_array('nofollow', $rel)) {
      continue;
    }

    $rel[] = 'nofollow';
    $anchor->setAttribute('rel', implode(' ', $rel));
}

var_dump($dom->saveHTML());

CodePad

生成的 HTML 位于 $dom->saveHTML() 中。除非它会用 htmlbody 元素等包裹它,所以用它来提取您输入的 HTML...

$html = '';

foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
    $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);
}

echo $html;

如果您有 >= PHP 5.3,请替换saveXML()saveHTML() 并删除第二个参数。

示例

此 HTML...

<a href="">hello</a>

<a href="" rel="">hello</a>

<a href="" rel="hello there">hello</a>

<a href="" rel="nofollow">hello</a>

...转换为...

<a href="" rel="nofollow">hello</a>

<a href="" rel="nofollow">hello</a>

<a href="" rel="hello there nofollow">hello</a>

<a href="" rel="nofollow">hello</a>

Regexs really aren't the best tool for dealing with HTML, especially when PHP has a pretty good HTML parser built in.

This code will handle adding nofollow if the rel attribute is already populated.

$dom = new DOMDocument;

$dom->loadHTML($str);

$anchors = $dom->getElementsByTagName('a');

foreach($anchors as $anchor) { 
    $rel = array(); 

    if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
       $rel = preg_split('/\s+/', trim($relAtt));
    }

    if (in_array('nofollow', $rel)) {
      continue;
    }

    $rel[] = 'nofollow';
    $anchor->setAttribute('rel', implode(' ', $rel));
}

var_dump($dom->saveHTML());

CodePad.

The resulting HTML is in $dom->saveHTML(). Except it will wrap it with html, body elements, etc, so use this to extract just the HTML you entered...

$html = '';

foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
    $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);
}

echo $html;

If you have >= PHP 5.3, replace saveXML() with saveHTML() and drop the second argument.

Example

This HTML...

<a href="">hello</a>

<a href="" rel="">hello</a>

<a href="" rel="hello there">hello</a>

<a href="" rel="nofollow">hello</a>

...is converted into...

<a href="" rel="nofollow">hello</a>

<a href="" rel="nofollow">hello</a>

<a href="" rel="hello there nofollow">hello</a>

<a href="" rel="nofollow">hello</a>
披肩女神 2024-11-11 12:53:54

好亚历克斯。如果它是函数的形式,那就更有用了。所以我做了如下:

function add_no_follow($str){ 
  $dom = new DOMDocument;

  $dom->loadHTML($str);

  $anchors = $dom->getElementsByTagName('a');

  foreach($anchors as $anchor) { 
      $rel = array(); 

      if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
         $rel = preg_split('/\s+/', trim($relAtt));
      }

      if (in_array('nofollow', $rel)) {
        continue;
      }

      $rel[] = 'nofollow';
      $anchor->setAttribute('rel', implode(' ', $rel));
  }

  $dom->saveHTML();

  $html = '';

  foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
      $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);
  }

  return $html;      
}

使用如下:

  $str = "Some content with link Some content  ... ";

 $str = add_no_follow($str);

Good Alex. If it is in the form of a function it is more useful. So I made it below:

function add_no_follow($str){ 
  $dom = new DOMDocument;

  $dom->loadHTML($str);

  $anchors = $dom->getElementsByTagName('a');

  foreach($anchors as $anchor) { 
      $rel = array(); 

      if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
         $rel = preg_split('/\s+/', trim($relAtt));
      }

      if (in_array('nofollow', $rel)) {
        continue;
      }

      $rel[] = 'nofollow';
      $anchor->setAttribute('rel', implode(' ', $rel));
  }

  $dom->saveHTML();

  $html = '';

  foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
      $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);
  }

  return $html;      
}

Use as follows :

  $str = "Some content with link Some content  ... ";

 $str = add_no_follow($str);
花开柳相依 2024-11-11 12:53:54

我复制了 Alex 的答案 并将其放入一个函数中,该函数使链接成为 nofollow 并在新选项卡/窗口中打开(并且添加了 UTF-8 支持)。我不确定这是否是最好的方法,但它有效(欢迎提供建设性意见):

function nofollow_new_window($str)
{
$dom = new DOMDocument;

$dom->loadHTML($str);

$anchors = $dom->getElementsByTagName('a');

foreach($anchors as $anchor)
    { 
    $rel = array(); 

    if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
       $rel = preg_split('/\s+/', trim($relAtt));
        }

    if (in_array('nofollow', $rel)) {
      continue;
        }

    $rel[] = 'nofollow';
    $anchor->setAttribute('rel', implode(' ', $rel));

    $target = array(); 

    if ($anchor->hasAttribute('target') AND ($relAtt = $anchor->getAttribute('target')) !== '') {
       $target = preg_split('/\s+/', trim($relAtt));
        }

    if (in_array('_blank', $target)) {
      continue;
        }

    $target[] = '_blank';
    $anchor->setAttribute('target', implode(' ', $target));
    }

$str = utf8_decode($dom->saveHTML($dom->documentElement));
return $str;
}

只需使用如下函数:

$str = '<html><head></head><body>fdsafffffdfsfdffff dfsdaff flkklfd aldsfklffdssfdfds <a href="http://www.google.com">Google</a></body></html>';

$str = nofollow_new_window($str);

echo $str;

I've copied Alex's answer and made it into a function that makes links nofollow and open in a new tab/window (and added UTF-8 support). I'm not sure if this is the best way to do this, but it works (constructive input is welcome):

function nofollow_new_window($str)
{
$dom = new DOMDocument;

$dom->loadHTML($str);

$anchors = $dom->getElementsByTagName('a');

foreach($anchors as $anchor)
    { 
    $rel = array(); 

    if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
       $rel = preg_split('/\s+/', trim($relAtt));
        }

    if (in_array('nofollow', $rel)) {
      continue;
        }

    $rel[] = 'nofollow';
    $anchor->setAttribute('rel', implode(' ', $rel));

    $target = array(); 

    if ($anchor->hasAttribute('target') AND ($relAtt = $anchor->getAttribute('target')) !== '') {
       $target = preg_split('/\s+/', trim($relAtt));
        }

    if (in_array('_blank', $target)) {
      continue;
        }

    $target[] = '_blank';
    $anchor->setAttribute('target', implode(' ', $target));
    }

$str = utf8_decode($dom->saveHTML($dom->documentElement));
return $str;
}

Simply use the function like this:

$str = '<html><head></head><body>fdsafffffdfsfdffff dfsdaff flkklfd aldsfklffdssfdfds <a href="http://www.google.com">Google</a></body></html>';

$str = nofollow_new_window($str);

echo $str;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文