RobbyBubble
RobbyBubble

Reputation: 65

Wrap HTML table cell text in span tags

Just trying to embrace the innerWords of some td-elements with <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>

The td-class views-field cant't be used for it. My current codes is:

<?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);
?>

Upvotes: 0

Views: 412

Answers (3)

Kerem
Kerem

Reputation: 11566

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>

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

array_walk($brc, function (&$elem) { $elem = "/" . preg_quote($elem) . "/"; });
echo preg_replace($brc, '<span class="brc">\0</span>', $match[0]);

The array_walk is just to add regex delimiters around the words and properly escape them, but you can also do this manually.

Upvotes: 0

freejosh
freejosh

Reputation: 11383

If $brc is an array of strings you want to wrap with the span you could loop through them and use str_replace:

foreach($brc as $str) {
    $match[0] = str_replace($str, '<span class="brc">'.$str.'</span>', $match[0]);
}

Upvotes: 1

Related Questions