Php 链接的正则表达式

Php 链接的正则表达式,php,regex,Php,Regex,我有一个带有“a”标记的文本。我必须添加一些新的标签和属性 看起来是这样的: 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.' ”一些测试。然而,这是一个考验。” 现在我必须得到: 'Some test <noindex><a rel="nofollow" href="site">here</a></no

我有一个带有“a”标记的文本。我必须添加一些新的标签和属性

看起来是这样的:

'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.'
”一些测试。然而,这是一个考验。”
现在我必须得到:

'Some test <noindex><a rel="nofollow" href="site">here</a></noindex>.'
'Yet <noindex><a rel="nofollow" href="site2">another</a></noindex> test.'
“一些测试。”
“还没有测试。”

用php有什么快速的方法可以做到这一点吗?谢谢。

类似的内容将涵盖大多数现实情况:

$text = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';

$regex = '%(<a\s)(.*?</a>)%i';
$replacement = '<noindex>$1rel="nofollow" $2</noindex>';

preg_replace($regex, $replacement, $text);
$text='一些测试。然而,这是一次考验。”;

$regex='%(类似的内容将涵盖大多数实际情况:

$text = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';

$regex = '%(<a\s)(.*?</a>)%i';
$replacement = '<noindex>$1rel="nofollow" $2</noindex>';

preg_replace($regex, $replacement, $text);
$text='Some test.but test';
$regex='%(
$string=preg_replace('~~msi',''$html);
$string=preg_replace(“~~msi”,“'$html”);

请记住,使用正则表达式解析HTML是个坏主意(您应该改用类似的方法),这应该可以做到:

$str = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';
echo preg_replace('/<a(.+?)<\/a>/', '<noindex><a$1</a></noindex>', $str);
// Some test <noindex><a href="site">here</a></noindex>. Yet <noindex><a href="site2">another</a></noindex> test.
$str='Some test.but test';
echo preg_替换('/。尚未测试。

请记住,使用正则表达式解析HTML是个坏主意(您应该改用类似的方法),这应该可以做到:

$str = 'Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.';
echo preg_replace('/<a(.+?)<\/a>/', '<noindex><a$1</a></noindex>', $str);
// Some test <noindex><a href="site">here</a></noindex>. Yet <noindex><a href="site2">another</a></noindex> test.
$str='Some test.but test';
echo preg_替换('/。尚未测试。

只是想给出DOMDocument()版本,因为传统的说法是“不要在HTML上使用正则表达式!!”。好吧,这是一个很好的说法,但是接下来呢!好吧,给你:

    // create a new DOMDocument
    $doc = new DOMDocument();

    // load the string into the DOM
    $doc->loadHTML('Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.');

    // since we are working with HTML fragments here, remove <!DOCTYPE 
    $doc->removeChild($doc->firstChild);            

    // likewise remove <html><body></body></html> 
    $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);

    //Loop through each <a> tag in the dom and wrap it with <noindex>
    foreach($doc->getElementsByTagName('a') as $link) {
        $parent = $link->parentNode;
        $ni = $doc->createElement('noindex');
        $ni->appendChild($link->cloneNode(true));
        $parent->replaceChild($ni, $link);
    } 

   echo $doc->saveHTML();
//创建一个新文档
$doc=新的DOMDocument();
//将字符串加载到DOM中
$doc->loadHTML('Some test.but test');
//因为我们在这里处理HTML片段,所以删除removeChild($doc->firstChild);
//同样移除
$doc->replaceChild($doc->firstChild->firstChild->firstChild,$doc->firstChild);

//循环浏览每一个

只是想给出DOMDocument()版本,因为传统的说法是“不要在HTML上使用正则表达式!!”。好吧,这是一个很好的说法,但是接下来呢!?好吧,给你:

    // create a new DOMDocument
    $doc = new DOMDocument();

    // load the string into the DOM
    $doc->loadHTML('Some test <a href="site">here</a>. Yet <a href="site2">another</a> test.');

    // since we are working with HTML fragments here, remove <!DOCTYPE 
    $doc->removeChild($doc->firstChild);            

    // likewise remove <html><body></body></html> 
    $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);

    //Loop through each <a> tag in the dom and wrap it with <noindex>
    foreach($doc->getElementsByTagName('a') as $link) {
        $parent = $link->parentNode;
        $ni = $doc->createElement('noindex');
        $ni->appendChild($link->cloneNode(true));
        $parent->replaceChild($ni, $link);
    } 

   echo $doc->saveHTML();
//创建一个新文档
$doc=新的DOMDocument();
//将字符串加载到DOM中
$doc->loadHTML('Some test.but test');
//因为我们在这里处理HTML片段,所以删除removeChild($doc->firstChild);
//同样移除
$doc->replaceChild($doc->firstChild->firstChild->firstChild,$doc->firstChild);

//循环遍历每个

你不能用正则表达式解析[X]HTML。但是你可以用结构良好的标记在PHP中进行正则表达式替换。这是一个有效的问题。你不能用正则表达式解析[X]HTML。但是你可以用结构良好的标记在PHP中进行正则表达式替换。这是一个有效的问题。