2017-04-05 36 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