帮助通过正则表达式获取值(php)

时间:2009-07-09 19:22:38

标签: php regex

所以,我用正则表达式吮吸。需要用PHP完成。谢谢。 我需要能够拉出“xx”(永远是2个小写字母字符)和“a12”(可以是任何东西,但永远都是.php)。

String:
http://foo.bar.com/some_directory/xx/a12.php?whatever=youwant

3 个答案:

答案 0 :(得分:1)

由于他正在寻找 PHP 解决方案而不仅仅是PCRE,我认为这样的事情可能会更全面:

$src = 'http://foo.bar.com/some_directory/xx/a12.php?whatever=youwant';
preg_match( '/([a-z]{2})\/([^\/]+)\.php/', $src, $matches );
/* grab "xx" */
$first = $matches[1];
/* grab "a12" */
$second = $matches[2];

答案 1 :(得分:0)

"([a-z]{2})\/([^/]+)\.php"

确保您正在捕捉比赛。 xx将在组1中,a12将在组2中

答案 2 :(得分:0)

/([A-Z] {2})/([A-ZA-Z0-9 _-] +)

$string = http://foo.bar.com/some_directory/xx/a12.php?whatever=youwant


$matches;
preg_match("/([a-z]{2})/([a-zA-Z0-9_\-]+)", $string, $matches);

$part_1 = $matches[1]; //xx
$part_2 = $matches[2]; //a12
祝你好运!