Text replacement: PHP/regex

后端 未结 2 2045
滥情空心
滥情空心 2021-01-23 23:38

I am presented with an HTML document similar to this in view source mode (the below is simplified for brevity):


    
        

        
2条回答
  •  独厮守ぢ
    2021-01-24 00:03

    My guess is that you are likely designing an expression similar to:

    <(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>
    

    which you might probably want to modify it, and then replace with what you like to replace.

    The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

    Test

    $re = '/<(?:textarea|select)[\s\S]*?({{variable:system_version}})[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>/m';
    $str = '
        
            System version: 6.0
        
        
            

    You are using system version 6.0

    This was the content of the welcome block.
    '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); var_dump($matches);

    RegEx Circuit

    jex.im visualizes regular expressions:


    Edit for two steps:

    <(?:textarea|select)[\s\S]*?>[\s\S]*?<\/(?:textarea|select)>|<(?:input)[\s\S]*?>
    

    Demo 1

    ^<(?:input)[\s\S]*?({{variable:system_version}})[\s\S]*?>$
    

    Demo 2

    ^<(?:input).*?({{variable:system_version}}).*?>$
    

    Demo 3

提交回复
热议问题