试图用<span class="brc">Words</span>
包含一些 td - 元素的内在词。
<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-class views-field 。我目前的代码是:
<?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);
?>
答案 0 :(得分:1)
如果$brc
是一个字符串数组,你想用span
包裹,你可以遍历它们并使用str_replace
:
foreach($brc as $str) {
$match[0] = str_replace($str, '<span class="brc">'.$str.'</span>', $match[0]);
}
答案 1 :(得分:0)
array_walk($brc, function (&$elem) { $elem = "/" . preg_quote($elem) . "/"; });
echo preg_replace($brc, '<span class="brc">\0</span>', $match[0]);
array_walk
只是在单词周围添加正则表达式分隔符并正确转义它们,但您也可以手动执行此操作。
答案 2 :(得分:0)
您可以使用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;
输出;
<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>