English 中文(简体)
说明与经常表达团体相对应的部分内容?
原标题:Extract parts of string that match regular expression groups?
  • 时间:2012-01-12 16:46:18
  •  标签:
  • php
  • regex

这并非完全有可能,但希望令人感到惊讶。

我有这样的定期表述:

$pattern =  #post/date/(dddd)-(dd)-(dd)# ;

而且,我甚至有这样的说法:

$string =  post/date/2012-01-01 ;

当然,我不知道确切的格局和前面的描述,但他们将照此看。

在结束发言时,我需要看到这样一个阵列:

$groups = array( 2012 ,  01 ,  01);

阵列中应包含与括号内三个经常表达团体相对应的部分。 这是可能的吗?

最佳回答

父母之间的这些事情是父辈,如果你提出申请,你可以取得结果。 您可使用“前言”

$string =  post/date/2012-01-01 ;
$pattern =  #post/date/(dddd)-(dd)-(dd)# ;
preg_match_all($pattern, $string, $matches);
$groups = array($matches[1][0], $matches[2][0],$matches[3][0]);
echo  <pre> ;
print_r($groups);
echo  </pre> ;

sure, this is an example that just shows the behavior, you will need to check first if the string matched the pattern and if it did, how many times.. you can see that piece of code working here: http://ideone.com/zVu75

问题回答

Not a regular expression but will do what you want to achieve

<?php
$string =  post/date/2012-01-01 ;
$date= basename($string);
$array=explode( - ,$date);
print_r($array);
?>

产出阵列将照此办理。

Array
(
    [0] => 2012
    [1] => 01
    [2] => 01
)
$str =  post/date/2012-01-01 ;
preg_match( #post/date/(d+)-(d+)-(d+)# , $str, $m);
array_shift($m);
$group = $m;
print_r($group);

产出

Array
(
    [0] => 2012
    [1] => 01
    [2] => 01
)

请回顾一下:

$matches = sscanf($string,  %*[^0-9]%d-%d-%d );

http://www.un.org。

array(3) {
  [0]=> int(2012)
  [1]=> int(1)
  [2]=> int(1)
}




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

热门标签