English 中文(简体)
Strange Regex Issue
原标题:Strange Regex Issue
  • 时间:2012-01-15 13:21:36
  •  标签:
  • php
  • regex

我认为,我可能忽视了这里的内容,但我不敢肯定为什么发生这种奇怪的行为。

我知道这是一个非意见性的reg,但我的问题是为什么发生这种情况。

<?php
// < 15 "a" characters returns "aaaaaaaaaaaaaa {" (no matches)
$code =  aaaaaaaaaaaaaa { ;
// 15 "a" characters returns NULL
$code =  aaaaaaaaaaaaaaa { ;

$code = preg_replace( #(a+)+{# ,  b , $code);
var_dump($code);
?>

我确信,该表有一份<条码>缺失。 如果我改为<代码>#(a+)+s{#。 页: 1 但为什么我拿不到<条码>,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{“,15个“a”特性,而不是NUL? 该手册指出,在出现错误时,NUL即予退回,但PHP没有印刷(正在核对)。

EDIT:

我使用<代码>(a+)+的原因是,我试图将问题简化到基本层面。 我开始这样做:

我想抓住一系列非白色空间特征,然后是空地,然后是开张。

this is some text {

因此,我使用了reg:

#([^s{}]+s*)+{#msi

后者还使用双重+。 是否有更好的办法,为什么首先造成一个问题?

EDIT 2: From the answers below, it appears that the double + causes a lot of backreferences and should be avoided. I have opted for this [^s{}][^{}]*+{.

最佳回答
<?php

ini_set( pcre.backtrack_limit ,20000);
$code =  aaaaaaaaaaaaaaa { ;
$code = preg_replace( #(a+)+{# ,  b , $code);
var_dump($code, preg_last_error());

ini_set( pcre.backtrack_limit ,1000000);
$code =  aaaaaaaaaaaaaaa { ;
$code = preg_replace( #(a+)+{# ,  b , $code);
var_dump($code, preg_last_error());

Result:

NULL
int(2)
string(17) "aaaaaaaaaaaaaaa {"
int(0)

但你确实应当使用<代码>(a+)或(a)+,而不是两者的结合。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签