0

只是试图用<span class="brc">Words</span>.

<td class="views-field views-field-summoner-name-1"> Zeit für ein dududuDUELL </td>
<td class="views-field views-field-summoner-name-1"> EloDrop </td>
<td class="views-field views-field-summoner-name-1"> HighPINGklärtGG </td>
<td class="views-field views-field-summoner-name-1"> BlaViShi </td>
<td class="views-field views-field-summoner-name-1"> Bruteforce tv </td>

td 类视图字段不能用于它。我目前的代码是:

<?php

$url = "http://competitive.euw.leagueoflegends.com/de/ladders/euw/current/ranked_team_3x3";

preg_match('#<table class="views-table cols-6"[^>]+>[\w\W]*?</table>#i', file_get_contents($url), $match);
echo $match[0];

$brc = array("Zeit für ein dududuDUELL","OP Scheisse","Selbstzerstörungsknopf","EloDrop","HighPINGklärtGG","BlaViShi");
echo preg_replace(I dont know how this works);
?>
4

3 回答 3

1

如果$brc是要包装的字符串数组,则span可以循环遍历它们并使用str_replace

foreach($brc as $str) {
    $match[0] = str_replace($str, '<span class="brc">'.$str.'</span>', $match[0]);
}
于 2013-01-27T22:49:08.527 回答
0

You can use preg_replace_callback;

$s = '<table class="views-table cols-6">
        <td class="views-field views-field-summoner-name-1"> Zeit für ein dududuDUELL </td>
        <td class="views-field views-field-summoner-name-1"> EloDrop </td>
        <td class="views-field views-field-summoner-name-1"> HighPINGklärtGG </td>
        <td class="views-field views-field-summoner-name-1"> BlaViShi </td>
        <td class="views-field views-field-summoner-name-1"> Bruteforce tv </td>
      </table>';
$s = preg_replace_callback('~<td(.*?)>(.*?)</td>~isu', function($m) {
    return sprintf('<td%s><span class="brc">%s</span></td>', $m[1], $m[2]);
}, $s);
print $s;

Output;

<table class="views-table cols-6">
    <td class="views-field views-field-summoner-name-1"><span class="brc"> Zeit für ein dududuDUELL </span></td>
    <td class="views-field views-field-summoner-name-1"><span class="brc"> EloDrop </span></td>
    <td class="views-field views-field-summoner-name-1"><span class="brc"> HighPINGklärtGG </span></td>
    <td class="views-field views-field-summoner-name-1"><span class="brc"> BlaViShi </span></td>
    <td class="views-field views-field-summoner-name-1"><span class="brc"> Bruteforce tv </span></td>
</table>
于 2013-01-28T02:27:13.690 回答
0
array_walk($brc, function (&$elem) { $elem = "/" . preg_quote($elem) . "/"; });
echo preg_replace($brc, '<span class="brc">\0</span>', $match[0]);

array_walk只是在单词周围添加正则表达式分隔符并正确转义它们,但您也可以手动执行此操作。

于 2013-01-27T22:49:31.787 回答