English 中文(简体)
如何改进这一规定?
原标题:How do i improve this regex?
  • 时间:2011-11-22 06:52:41
  •  标签:
  • php
  • regex

我有以下几部PHP代码,用于配对一年:

$pattern = "/20dd/";

$exp = "BORN ON:        Friday, June 11, 2001 ";

$match = preg_match($pattern,$exp, $matches);

echo $matches[0];

This matches "2001"...which is what I am trying to match--the birth year. But this is clearly a hack of a solution and can run into problems if there are other years before $exp for some reason. How can I include the "BORN ON" information to pick out just the year?

最佳回答

还搜索“BORN ON”并利用一个捕获组。

$pattern = "/BORN ON.*(20dd)s$/";
$exp = "BORN ON:        Friday, June 11, 2001 ";
$match = preg_match($pattern,$exp, $matches);
echo $matches[1];

由于现在的方括号,我把国际年归入一个捕获组,你将这个组带上<代码>$matches >> (而不是0)。 此外,我把年限到座尾(代号为:>>,与$

问题回答

为此:

 /BORN ON:s+w+, w+ d+, (20dd)/ 

首先,所有投入数据都实现正常化。 如果你重新划分为固定宽度或采用具体、简单的格式,那么就会注意到,并开发出一种能够更好地适应投入数据的可操作性。 如您需要快速参考,请查询





相关问题
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 ...

热门标签