douhuan1257 2016-10-10 18:13
浏览 18

如何在<a>标签之间的所有行添加标签[重复]

This question already has an answer here:

$update_text= preg_replace("/(<a[^>]*>)\s*(.*?)\s*(<\/a>)/s", "$1
\t$2
$3", $text);

Have this :

<a href="#" class="example">
<img src="bg1.jpg">
text text
</a>
<a href="#" class="example">
text text
<img src="bg2.jpg">
text text
</a>
<a href="#" class="example">
<img src="bg3.jpg">
</a>

Want this :

<a href="#" class="example">
  <img src="bg1.jpg">
  text text
</a>
<a href="#" class="example">
  text text
  <img src="bg2.jpg">
  text text
</a>
<a href="#" class="example">
  <img src="bg3.jpg">
</a>

https://regex101.com/r/qo2N22/3

I want to add \t all $2 lines but only applied to the first line. Could anyone help me? Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dsrjs86444 2016-10-10 20:49
    关注

    I did it with preg_replace_callback. Thanks @chris85 for suggestion.

    $update_text= preg_replace_callback("/(<a[^>]*>)\s*(.*?)\s*(<\/a>)/s", function($match) {
          $values = explode("
    ",$match[2]);
          foreach ($values as $value) {
              $newmatch .= "
    \t".$value;
          }
          return $match[1].$newmatch."
    ".$match[3];
        }, $text);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比