PHP:正则表达式匹配以下字符串示例

时间:2011-07-03 01:53:52

标签: php regex string expression match

我一直在尝试为我的语法荧光笔调整一个完美的字符串匹配表达式,但现在是时候寻求帮助了。

<?php

    if ( !empty( $_GET['gamertag'] ) )
    {

        require_once( 'xbox.php' );
        header( 'Content-Type: image/png' );

        // Content
        $xbox = new Xbox( $_GET['gamertag'] );
        $font = 'fonts/helr65w.ttf';

        // Images
        $bg = @imagecreatefrompng( 'content/gamercard.png' );
        $gp = @imagecreatefrompng( $xbox->Links['GamerPicture'] );
        $st = @imagecreatefrompng( 'content/star.png' );
        $re = Array();

        if ( $xbox->CanViewGames() )
        {

            foreach( $xbox->RecentGames as $key => $value )
                $re[] = @imagecreatefromjpeg( $value['Image'] );

        }

        // Save Transparency
        @imagesavealpha( $bg, true ); 
        @imagealphablending( $bg, false ); 
        @imagecolorallocatealpha( $bg, 255, 255, 255, 127 );
        @imagealphablending( $bg, true );

        // Create Colors
        $white = @imagecolorallocate( $bg, 255, 255, 255 );
        $grey = @imagecolorallocate( $bg, 128, 128, 128 );
        $black = @imagecolorallocate( $bg, 0, 0, 0 );
        $orange = @imagecolorallocate( $bg, 233, 171, 23 );

        // Write Information

        for( $i = 0; $i < count( $re ); $i++ ) // Recent Games
            @imagecopy( $bg, $re[$i], ( 100 + ( 40 * $i ) ), 44, 0, 0, 32, 32 );

        for( $r = 0; $r < $xbox->Reputation; $r++ ) // Reputation
            @imagecopy( $bg, $st, ( 196 + ( $r * 20 ) ), 125, 0, 0, 16, 16 );

        @imagecopy( $bg, $gp, 18, 55, 0, 0, 64, 64 );
        @imagettftext( $bg, 14, 0, 40, 30, $black, $font, $xbox->Gamertag );
        @imagettftext( $bg, 14, 0, 143, 105, $black, $font, $xbox->Gamerscore );
        @imagettftext( $bg, 6, 0, 7, 161, $black, $font, $xbox->CurrentActivity );
        @imagepng( $bg );

        // Destroy Images
        @imagedestroy( $bg );
        @imagedestroy( $gp );
        @imagedestroy( $st );

        foreach( $re as $game )
            @imagedestroy( $game );

    }
    else
    {

        print( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    <head>

        <title>Xbox Gamercard</title>

        <script type="text/javascript">

            function go()
            {

                var gamertag = document.getElementById( "gamertag" ).value;
                window.location.href = ( gamertag + ".png" );

            }

        </script>

    </head>

    <body>

        <input type="text" id="gamertag" onkeydown="if (event.keyCode == 13) go();" />
        <input type="button" value="Submit" onclick="go();" /><br /><br />

        <a href="/projects/xbox-gamercard/ttg/" title="TTG Signature Gamercard">TTG Signature Gamercard</a>

    </body>

</html>' );

    }

?>

我正在寻找能够成功匹配''""之间所有内容的表达式。

它需要忽略所有(正确)转义的自身版本。 (因此,''之间的内容将忽略\'""之间的内容将忽略\"

哪一个首先出现并不重要。

以下是我已尝试过的两个表达方式:

"/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*?'/"
"/'(.[^']*)'/"

1 个答案:

答案 0 :(得分:0)

试试这个原始正则表达式。第2组将包含您的字符串。

(['"])((?:\\\1|(?!\1).)+)\1

由于PHP不是我的语言,我不确定我是否正确地逃脱了它,但试试这个:

$pattern = '/([\'"])((?:\\\\\\1|(?!\\1).)+)\\1/';

这个不会适当地捕获注释代码,例如:

// ' my string does not get terminated.