Search code examples
phpregexpreg-match

regex to match whatever between


Please, I need some help to create the right regex.

I want to detect whatever between Decode(" ") to give me this output 2%65%66%_WHATEVER_8%74%74

I tried a lot, but nothing works correctly to give me the exact output that I want.

My code:

$string = '
    <td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
    <td class="green"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
    <td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
';
$pattern = '/Decode("([^*]+)")/i';
preg_match_all($pattern, $string, $matches);

print_r($matches[1]);

Solution

  • As stated in the comments, you could use

    Decode\("([^"]+)"\)
    

    And take the first group, see a demo on regex101.com.


    As PHP demo:

    <?php
    
    $data = <<<DATA
    <script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script>
    DATA;
    
    $regex = '~Decode\("([^"]+)"\)~';
    
    if (preg_match_all($regex, $data, $matches)) {
        print_r($matches[1]);
    }
    
    ?>