Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t获取preg_match以返回所需的数据_Php_Regex_Preg Match - Fatal编程技术网

Php Can';t获取preg_match以返回所需的数据

Php Can';t获取preg_match以返回所需的数据,php,regex,preg-match,Php,Regex,Preg Match,好吧,假设我需要参与这个链接:http://www.dailymotion.com/video/xnstwx_bmw-eco-pro-race_auto 我需要去 [0]=>xnstwx [1]=>bmw-eco-pro-race\U auto 我正在尝试:preg_match('/video\/([A-Za-z0-9]+)_/I',$video['url'],$match) 我得到: [0]=>video/xnstwx_979; [1]=>xnstwx 然后我尝试使用:preg_match('

好吧,假设我需要参与这个链接:
http://www.dailymotion.com/video/xnstwx_bmw-eco-pro-race_auto

我需要去

[0]=>xnstwx

[1]=>bmw-eco-pro-race\U auto

我正在尝试:
preg_match('/video\/([A-Za-z0-9]+)_/I',$video['url'],$match)

我得到:

[0]=>video/xnstwx_979;

[1]=>xnstwx

然后我尝试使用:
preg_match('/video\/([A-Za-z0-9]+)。/([A-Za-z0-9-+),$video['url',$match)
我想你已经知道这是错误的

我总是无缘无故地使用正则表达式,现在我正试图学习使用正则表达式备忘单,但现在我有点卡住了:)。

之后添加
(.+)
。这将捕获任何字符中的一个或多个,这些字符按
()
之后分组

preg_match('/video\/([A-Za-z0-9]+)_(.+)/i', $video['url'], $match);

var_dump($match);
array(3) {
  [0]=>
  string(34) "video/xnstwx_bmw-eco-pro-race_auto"
  [1]=>
  string(6) "xnstwx"
  [2]=>
  string(21) "bmw-eco-pro-race_auto"
}
有很多可能的方法可以做到这一点,但这只是我想到的第一个例子。

之后添加
(.+)
。这将捕获任何字符中的一个或多个,这些字符按
()
之后分组

preg_match('/video\/([A-Za-z0-9]+)_(.+)/i', $video['url'], $match);

var_dump($match);
array(3) {
  [0]=>
  string(34) "video/xnstwx_bmw-eco-pro-race_auto"
  [1]=>
  string(6) "xnstwx"
  [2]=>
  string(21) "bmw-eco-pro-race_auto"
}
preg_match('@video/([^_]+)_(.+)@', $video['url'], $match);
有很多潜在的方法可以做到这一点,但这只是我想到的第一个例子

preg_match('@video/([^_]+)_(.+)@', $video['url'], $match);
还有一个提示:在处理URL时,最好不要使用
/
作为正则表达式分隔符,这样就不必逃避模式中的所有斜杠


还有一个提示:在处理URL时,最好不要使用
/
作为正则表达式分隔符,这样就不必转义模式中的所有斜杠。

使用以下命令获得$match[1]和$match[2]中的匹配项

preg_match("/.*\/video\/([a-z0-9]+)_(.*)$/i", $video['url'], $match);
不需要带i修饰符的A-Z,数组中的第一个元素始终是完全匹配的表达式,这就是预期结果位于位置1和2的原因

问候,, 菲尔


编辑:没有意识到我必须使用代码标签来显示转义斜杠

使用以下命令获取$match[1]和$match[2]中的匹配项

preg_match("/.*\/video\/([a-z0-9]+)_(.*)$/i", $video['url'], $match);
不需要带i修饰符的A-Z,数组中的第一个元素始终是完全匹配的表达式,这就是预期结果位于位置1和2的原因

问候,, 菲尔

编辑:没有意识到我必须使用代码标签来显示转义斜杠

有点晚了,但是:

<?php
$u = 'http://www.dailymotion.com/video/xnstwx_bmw-eco-pro-race_auto';
preg_match('/([A-Za-z0-9]+)_([A-Za-z0-9].+)/', $u,$m);
print_r($m);
?>
有点晚了,但是:

<?php
$u = 'http://www.dailymotion.com/video/xnstwx_bmw-eco-pro-race_auto';
preg_match('/([A-Za-z0-9]+)_([A-Za-z0-9].+)/', $u,$m);
print_r($m);
?>