【发布时间】:2011-07-03 01:53:52
【问题描述】:
我一直在尝试为我的语法荧光笔调整一个完美的字符串匹配表达式,但现在是寻求帮助的时候了。
<?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>' );
}
?>
我正在寻找可以成功匹配'' 和"" 之间所有内容的表达式。
它需要忽略所有(正确)转义的自身版本。 (所以''之间的内容会忽略\',""之间的内容会忽略\")
谁先来并不重要。
这是我已经尝试过的两种表达方式:
"/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*?'/"
"/'(.[^']*)'/"
【问题讨论】:
-
举个例子,StackOverflow 可以正确识别我的示例中的所有字符串。
-
StackOverflow 使用语法高亮显示
-
干净版本(不在 php 字符串中):'([^]*.)*'(警告:未经测试,我假设 \
仅转义) -
没什么问题:
"/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*?'/"但你可能想设置 dot-matches-all 模式并像这样失去惰性修饰符:"/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'/s" -
你的权利,它正在工作。我的代码有问题...
标签: php regex string expression match