这段代码有什么不对

时间:2011-08-24 13:05:22

标签: php

<?php

$html = 'sdfknjasdf kjfdasj fjknadssajkndf kjadsknajsdfjndfasjk <tr class="vcard agent">
<span class="fn"><a href="/wiki/Brad_Jones_(footballer)" title="Brad Jones (footballer)">Brad Jones</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Glen_Johnson_(English_footballer)" title="Glen Johnson (English footballer)">Glen Johnson</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jos%C3%A9_Enrique_S%C3%A1nchez" title="José Enrique Sánchez">José Enrique</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Raul_Meireles" title="Raul Meireles">Raul Meireles</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Daniel_Agger" title="Daniel Agger">Daniel Agger</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/F%C3%A1bio_Aur%C3%A9lio" title="Fábio Aurélio">Fábio Aurélio</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Luis_Su%C3%A1rez_(Uruguyan_footballer)" title="Luis Suárez (Uruguyan footballer)" class="mw-redirect">Luis Suárez</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Steven_Gerrard" title="Steven Gerrard">Steven Gerrard</a></span> <i>(<a href="/wiki/Captain_(association_football)" title="Captain (association football)">captain</a>)</i></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Andy_Carroll" title="Andy Carroll">Andy Carroll</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Joe_Cole" title="Joe Cole">Joe Cole</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Maxi_Rodr%C3%ADguez" title="Maxi Rodríguez">Maxi Rodríguez</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Daniel_Pacheco" title="Daniel Pacheco">Daniel Pacheco</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jordan_Henderson" title="Jordan Henderson">Jordan Henderson</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Alberto_Aquilani" title="Alberto Aquilani">Alberto Aquilani</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Dirk_Kuyt" title="Dirk Kuyt">Dirk Kuyt</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Stewart_Downing" title="Stewart Downing">Stewart Downing</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jay_Spearing" title="Jay Spearing">Jay Spearing</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Lucas_Leiva" title="Lucas Leiva">Lucas Leiva</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Danny_Wilson_(Scottish_footballer)" title="Danny Wilson (Scottish footballer)">Danny Wilson</a></span></td>
</tr>

</table>

</td>

<td width="1%"></td>
<td bgcolor="#FFFFFF" valign="top" width="48%">
<table border="0" cellspacing="0" cellpadding="2">

<tr bgcolor="#AAD0FF">
<th width="1%">No.</th>

<th width="1%"></th>
<th width="1%">Position</th>
<th width="75%">Player</th>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jamie_Carragher" title="Jamie Carragher">Jamie Carragher</a></span> <i>(<a href="/wiki/Captain_(association_football)#Vice-captain" title="Captain (association football)">vice-captain</a>)</i></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/David_N%27Gog" title="David N'Gog">David N'Gog</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jos%C3%A9_Manuel_Reina" title="José Manuel Reina">Pepe Reina</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Charlie_Adam" title="Charlie Adam">Charlie Adam</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Christian_Poulsen" title="Christian Poulsen">Christian Poulsen</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn">Jesús Fernández Sáez (Suso)</span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Raheem_Sterling" title="Raheem Sterling">Raheem Sterling</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Doni%C3%A9ber_Alexander_Marangon" title="Doniéber Alexander Marangon">Doni</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jonjo_Shelvey" title="Jonjo Shelvey">Jonjo Shelvey</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Martin_Kelly" title="Martin Kelly">Martin Kelly</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Conor_Coady" title="Conor Coady">Conor Coady</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Martin_%C5%A0krtel" title="Martin Škrtel">Martin Škrtel</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/John_Flanagan_(English_footballer)" title="John Flanagan (English footballer)">John Flanagan</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Nathan_Eccleston" title="Nathan Eccleston">Nathan Eccleston</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn">Andre Wisdom</span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Jack_Robinson_(footballer_born_1993)" title="Jack Robinson (footballer born 1993)">Jack Robinson</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/David_Amoo" title="David Amoo">David Amoo</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Philipp_Degen" title="Philipp Degen">Philipp Degen</a></span></td>
</tr>

<tr class="vcard agent">
<td><span class="fn"><a href="/wiki/Emiliano_Ins%C3%BAa" title="Emiliano Insúa">Emiliano Insúa</a></span></td>
</tr>

</table>';

$first = strpos($html, '<tr class="vcard agent">');
$result = substr($html, $first);

echo $result;

?>

我想删除

  

sdfknjasdf kjfdasj fjknadssajkndf kjadsknajsdfjndfasjk

第一次出现<tr class="vcard agent">

之前的

3 个答案:

答案 0 :(得分:3)

对于大量文本,包含引号使用HEREDOC syntax

$html = <<<EOT
sdfknjasdf kjfdasj fjknadssajkndf kjadsknajsdfjndfasjk <tr class="vcard agent">
<span class="fn"><a href="/wiki/Brad_Jones_(footballer)" title="Brad Jones (footballer)">Brad Jones</a></span></td>
</tr>
EOT;

答案 1 :(得分:1)

使用反斜杠转义所有单引号。

示例:

$html = '[...] <a href="/wiki/David_N%27Gog" title="David N'Gog">David N'Gog</a> [...]';

不正确,而

$html = '[...] <a href="/wiki/David_N%27Gog" title="David N\'Gog">David N\'Gog</a> [...]';

是对的。

答案 2 :(得分:0)

查找...

<td><span class="fn"><a href="/wiki/David_N%27Gog" title="David N'Gog">David N'Gog</a></span></td>

替换为......

<td><span class="fn"><a href="/wiki/David_N%27Gog" title="David N\'Gog">David N\'Gog</a></span></td>

您的代码在两个地方包含未转义的单引号。