V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
cnqncom
V2EX  ›  PHP

问个正则规则的写法

  •  
  •   cnqncom · Oct 17, 2017 · 3987 views
    This topic created in 3124 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
        1
    lrvy  
       Oct 17, 2017
    \bbrush\b:[php|java]; 这样吗?
    cnqncom
        2
    cnqncom  
    OP
       Oct 17, 2017
    @lrvy 可能不止这些,我希望里面是任意字符串
    2ME
        3
    2ME  
       Oct 17, 2017
    用 phpQuery 提取出来 CLASS 简单一点
    askfilm
        4
    askfilm  
       Oct 17, 2017
    @cnqncom 把 “;” 换成 “;toolbar:false;” 不就行了,
    或者你不放心的话 把 “ ;' ” 换成 “ ;toolbar:false;' ” 也行啊
    要啥正则
    whahugao
        5
    whahugao  
       Oct 17, 2017   ❤️ 1
    echo preg_replace('/brush:([^\s\']+)/i', "brush:$1toolbar:false;","<div class='brush:php;'>");
    cnqncom
        6
    cnqncom  
    OP
       Oct 17, 2017
    @askfilm 这样不行哦,其他 CLASS 全部被替换了
    askfilm
        7
    askfilm  
       Oct 17, 2017
    @cnqncom 什么 class 里会分号?
    hadixlin
        8
    hadixlin  
       Oct 17, 2017
    brush:(.*)?; brush:$1;toolbar:false
    @whahuzhihao 的答案应该就是正解了.

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

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