2017-04-05 49 views
0

例子:正則表達式的URL鏈接的html等內容

Access – <a href="http://extratorrent.cc/search/?search=Cotent">Torrent Search</a> – <a href="http://link1.com/Text">Link 1</a> – <a href="http://link2.com/Text">Link 2</a> – <a href="http://link3.com/Text">Link 3</a> – <a href="http://link4.com/Text">Link 4</a> 

我想用正則表達式來使它像這樣

Access – <a href="http://extratorrent.cc/search/?search=Cotent">Torrent Search</a> – AAABBBCCC 

我只知道</?a(|\s+[^>]+)>刪除URL鏈接,但不知道如何正則表達式像上面的內容。

回答

0

像這樣的東西?

<?php 
$input = 'Access – <a href="http://extratorrent.cc/search/?search=Cotent">Torrent Search</a> – <a href="http://link1.com/Text">Link 1</a> – <a href="http://link2.com/Text">Link 2</a> – <a href="http://link3.com/Text">Link 3</a> – <a href="http://link4.com/Text">Link 4</a>'; 

$output = preg_replace("/<a href=\"http:\/\/(?!extratorrent.cc)[^\"]+\">([^<]+)<\/a>/", '$1', $input); 
// remove all links 
$output = preg_replace("/\ –? Link [0-9]+/", '', $output); 
// CONCAT TEXT 
$output.= " AABBCCDD"; 

echo $output; 

/* 
output: 
Access – <a href="http://extratorrent.cc/search/?search=Cotent">Torrent Search</a> AABBCCDD 
*/ 
+0

你最好。但是「/ /刪除所有鏈接期望第一」錯了我的意思,我想刪除所有後「... Torrent Search」,並添加一些文字後,如「... Torrent Search - AAABBBCCC」 – user7819129

+0

@ user7819129我有編輯答案,如果沒關係,請檢查「V」並放好投票,謝謝 – ZiTAL

+1

非常感謝! – user7819129