开发者

PHP: Regex to match the following string samples

I've been constantly attempting to adapt a perfect string matching expression for my syntax highlighter, but it's time to ask for help.

<?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
        开发者_如何学JAVA    @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>' );

    }

?>

I'm looking for an expression that can successfully match all content between '' and "".

It needs to ignore all (correctly) escaped versions of themselves. (So content between '' will ignore \', and content between "" will ignore \")

It doesn't matter which one comes first.

Here are two expressions I have already tried:

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


Try this raw regex. Group 2 would contain your strings.

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

Since PHP isn't my language I'm not sure I escaped it correctly, but try this:

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

This one won't catch commented code appropriately, for example:

// ' my string does not get terminated.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜