【问题标题】:preg_replace wordspreg_replace 单词
【发布时间】:2013-01-27 22:41:41
【问题描述】:

只是想用<span class="brc">Words</span> 来包含一些 td 元素的 innerWords。

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

【问题讨论】:

    标签: php regex arrays string preg-replace


    【解决方案1】:

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

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

    【讨论】:

    • 您应该可以用我的代码替换您的preg_replace 行,match[0] 将包含最终字符串。
    【解决方案2】:
    array_walk($brc, function (&$elem) { $elem = "/" . preg_quote($elem) . "/"; });
    echo preg_replace($brc, '<span class="brc">\0</span>', $match[0]);
    

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

    【讨论】:

      【解决方案3】:

      你可以使用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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-21
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多