0

我有以下字符串,我想删除以下标签之间的所有内容,包括这些标签:

<br> and the </span>

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a>

我已经尝试过preg_replace('/<br>.*?</a>/', '', $link),但这似乎删除了href ...

有什么想法我应该怎么做?

编辑: 使用后:

 preg_replace('/<br>.*?<\/span>/', '', $link) 

我现在在源代码中看到:

 <tr>
    <td><a class='interactive' href='http://www.proteinatlas.org/' target='_blank' alt='The protein atlas' >The protein atlas<br><span class='attribute'> - Uppsala Univeristät</td> 
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='%3Ca+class%3D%27interactive%27+href%3D%27http%3A%2F%2Fwww.proteinatlas.org%2F%27+target%3D%27_blank%27+alt%3D%27The+protein+atlas%27+%3EThe+protein+atlas%3Cbr%3E%3Cspan+class%3D%27attribute%27%3E+-+Uppsala+Univerist%C3%A4t' data_topic='161' data_introduction=''><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td>
  </tr>
  <tr>
    <td> funded by the Knut and Alice Wallenberg Foundation</span></a></td> 
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='+funded+by+the+Knut+and+Alice+Wallenberg+Foundation%3C%2Fspan%3E%3C%2Fa%3E' data_topic='161' data_introduction=''><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td>
  </tr>

编辑: 也试过了;

preg_replace('/<br><span class=\'attribute\'>.*?<\/span>/', '', $link)

但问题仍然存在。

编辑

仍然看到源显示为:

<a class='interactive' href='http://www.tinyurl.com/immunologygame/' target='_blank' alt='Innate Immunology game' >Innate Immunology game<br><span class='attribute'> - University of Ballarat</span></a>
4

3 回答 3

1

试试这个 :

<?php

$str = "<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a>";

$r = '/<br>(.+?)<\/span>/';

$str = preg_replace($r, '', $str);

echo $str;

?>

输出 :

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria</a>

演示: http ://regexr.com?33s84

于 2013-02-22T07:48:16.013 回答
1

试试这个

$str = "<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a>";

echo htmlspecialchars(preg_replace('#(<a[^>]+?>)([^<>]+).*#i', '$1$2</a>', $str));
于 2013-02-22T10:09:04.700 回答
0

只需使用这个简短的模式:

/<br>.*?<\/span>/

输出将是这样的:

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteriabla</a> 
于 2013-02-22T07:49:53.503 回答