cnqncom
V2EX  ›  PHP

问个正则规则的写法

  •  
  •   cnqncom · Oct 17, 2017 · 4479 views
    This topic created in 3306 days ago, the information mentioned may be changed or developed.
    $str = "<div class='brush:php;'>";
    $key = "brush:(a-z,A-Z);";
    $val = "brush:$1;toolbar:false;";
    echo preg_replace('/'.$key.'/i',$val,$str);



    brush:php;这里可能是 PHP 也可能是 JAVA 等
    需要将 brush:php;这个内容保留,在后面加上 toolbar:false;字符串
    15 replies  •  2017-10-17 18:01:27 +08:00
    lrvy
    lrvy
    Oct 17, 2017
    1
    \bbrush\b:[php|java]; 这样吗?
    cnqncom
    cnqncom
    OP
    Oct 17, 2017
    2
    @lrvy 可能不止这些,我希望里面是任意字符串
    2ME
    2ME
    Oct 17, 2017
    3
    用 phpQuery 提取出来 CLASS 简单一点
    askfilm
    askfilm
    Oct 17, 2017
    4
    @cnqncom 把 “;” 换成 “;toolbar:false;” 不就行了,
    或者你不放心的话 把 “ ;' ” 换成 “ ;toolbar:false;' ” 也行啊
    要啥正则
    whahugao
    whahugao
    Oct 17, 2017❤️1
    5
    echo preg_replace('/brush:([^\s\']+)/i', "brush:$1toolbar:false;","<div class='brush:php;'>");
    cnqncom
    cnqncom
    OP
    Oct 17, 2017
    6
    @askfilm 这样不行哦,其他 CLASS 全部被替换了
    askfilm
    askfilm
    Oct 17, 2017
    7
    @cnqncom 什么 class 里会分号?
    hadixlin
    hadixlin
    Oct 17, 2017
    8
    brush:(.*)?; brush:$1;toolbar:false
    @whahuzhihao 的答案应该就是正解了.

    另外楼主你的需求描述不够清楚,请注意.
    vincenth520
    vincenth520
    Oct 17, 2017
    9
    ```php
    <?php

    $str = "<div class='brush:php;'>";
    $key = "brush:(.+);";
    $val = "brush:$1;toolbar:false;";
    echo preg_replace('/'.$key.'/i',$val,$str);
    ```
    vincenth520
    vincenth520
    Oct 17, 2017
    10
    不支持 markdown,尴尬了
    hibobby
    hibobby
    Oct 17, 2017
    11
    为啥不把"<div class='brush:php;'>";中的 class='brush:php;'提取出来然后分组操作?
    cnqncom
    cnqncom
    OP
    Oct 17, 2017 via Android
    13
    @hadixlin 感谢
    sucaihuo
    sucaihuo
    Oct 17, 2017
    14
    进来学习一下
    renminghao
    renminghao
    Oct 17, 2017
    15
    /brush:(\w+?)/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1344 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 22:08 · PVG 06:08 · LAX 14:08 · JFK 17:08
    ♥ Do have faith in what you're doing.