如何查找字符串是否未被<a> tag</a>包围

时间:2012-02-15 10:59:53

标签: html regex

如何为未被锚标记(a)

包围的字符串创建正则表达式

例如:

this is <a href="#">sample link</a> this is sample link testing.

这一行包含2&#34;示例链接&#34;但我只需要获得第二个&#34;示例链接&#34;这不是(锚)标签所围绕的。

1 个答案:

答案 0 :(得分:0)

<?php

 $dom = new DOMDocument();
    //here goes the content even the links also you can have in your content 
    $content = 'this is <a href="#">sample link</a> this is sample link testing is sample link testing
        is sample link testing is sample link testing is sample link <a href="#">sample link</a> testing labs
        hello extended and link behave as if soon and post';


    $dom->loadHtml($content);
    $xml = simplexml_import_dom($dom);
    $data  = $xml->xpath('//body');
    $link  = $xml->xpath('//body/p/a');

    $resultLink = preg_split("/".$link[0][0]."/",$content, -1, PREG_SPLIT_OFFSET_CAPTURE);

    foreach($resultLink as $key=>$value):

        echo $resultLink[$key][0]."<a href='sample.html'>".$link[0][0]."</a>";

    endforeach;
?>