Web抓取URL包含最终标记如何删除

时间:2017-11-21 10:45:49

标签: php html regex parsing

所以我有这个代码来解析网站上的网址链接,但它在网址的末尾包含了</a>结束标记,如http://www2.enekoshop.jp/shop/tadaseinikuten/</a>

$html = new simple_html_dom();
$html->load($xml->retdata);

$item = array();

foreach($html->find('body') as $home) {
    //some other fields here
    $email1 = preg_replace('/[^\00-\255]+/u','',trim($home->find('div[id="home"] div[id="mainblock"] div[class="txtblock"]', 8)->children(1)->plaintext));

    $email = filter_var($email1, FILTER_SANITIZE_EMAIL);

    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
        $item['email'] = $email;
    } else {
        $item['email'] = NULL;
    }
}

虽然我使用plaintext来抓取文字,但它有</a>结束标记。我该如何删除它?

1 个答案:

答案 0 :(得分:1)

如何稍微扩展您的preg_replace()模式?

旧图案:/[^\00-\255]+/u

新模式:~[^\00-\255]+|</a>$~u

Pattern & Replace Demo Link

我只是更改您的模式分隔符,以避免在</a>中转义正斜杠并添加&#34;替代&#34; (| =&#39;或&#39;)仅使用</a>锚点在字符串末尾匹配$

$email1 = preg_replace('~[^\00-\255]+|</a>$~u','',trim($home->find('div[id="home"] div[id="mainblock"] div[class="txtblock"]', 8)->children(1)->plaintext));
相关问题