dongtiannai0654 2014-05-22 15:10
浏览 195
已采纳

正则表达式:匹配所有内容:直到 或<br />

I would like to change the following text

тест: фыв, фыв <br />
тест: фыв, фыв, фывфыв фыв, фыв, фыв <br />
счет: 123 <br />
высота: 111 <br />
конец: ок <br />

to:

тест: :: фыв, фыв :: <br />
тест: :: фыв, фыв, фывфыв фыв, фыв, фыв :: <br />
счет: :: 123 :: <br />
высота: :: 111::  <br />
конец: :: ок :: <br />

what I've tried:

$text = <<<HTML
    тест<br /><br />тест: фыв, фыв<br />тест: фыв, фыв, фывфыв фыв, фыв, фыв<br /><br />счет: 123<br />высота: 111 <br />конец: ок<br /><br />
HTML;

$text = preg_replace('~(\w+:)\s*(.*?)(\<br /\>|\R)~', '\1 :: \2 :: \3', $text);

echo $text;
  • 写回答

1条回答 默认 最新

  • dplase3140 2014-05-22 15:18
    关注

    RegEx:

    ([^:]+:)\s*(.*?)(<br\s*/>|\R)
    \1 :: \2 :: \3
    

    PHP:

    $row['text'] = preg_replace('~([^:]+:)\s*(.*?)(<br\s*/>|\R)~', '\1 :: \2 :: \3', $row['text']);
    

    Explanation: First ([^:]:) we match and capture 1+ non-colon characters followed by a colon. This is followed by some optional whitespace (\s*). Then we lazily capture 0+ characters (.*?). Finally we capture either the line break (<br\s*/>) or the new line (\R).

    Notes: I used ~ delimiters so that we didn't need to escape the / in the line break. We use lazy repetition for the second capture group because it lets us grab everything until the line break or new line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?