Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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,preg_匹配,正则表达式。我做错了什么?_Php_Regex_Preg Match - Fatal编程技术网

PHP,preg_匹配,正则表达式。我做错了什么?

PHP,preg_匹配,正则表达式。我做错了什么?,php,regex,preg-match,Php,Regex,Preg Match,以下是我想要匹配的模式: <div class="class"> <a href="http://www.example.com/something"> I want to be able to capture this text</a> <span class="ptBrand"> 这就是我正在做的: $pattern='{<div class="productTitle">[\n]<((https?|ftp|gopher

以下是我想要匹配的模式:

<div class="class">
<a href="http://www.example.com/something"> I want to be able to capture this text</a>
<span class="ptBrand">

这就是我正在做的:

$pattern='{<div class="productTitle">[\n]<((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)>([^\n]*)</a>[\n]<span class="ptBrand">}';

preg_match($pattern, $data, $matches,PREG_OFFSET_CAPTURE);

print_r($matches);
$pattern='{[\n]([^\n]*)[\n]}';
预匹配($pattern,$data,$matches,预偏移捕获);
打印(匹配项);
它打印:


数组()

一般来说,正则表达式是解析HTML非常糟糕的方法。它们是不可靠的,往往最终变得非常复杂。一个更健壮的解决方案是使用HTML解析器。看


至于表达式,我不认为正则表达式是解析HTML的一种非常糟糕的方法。它们是不可靠的,往往最终变得非常复杂。一个更健壮的解决方案是使用HTML解析器。看

至于你的表达,我看不出模式:

/\s*模式:

/\s*您可以尝试以下方法:

<a href=".*?">([\s\S]*?)</a>

您可以尝试以下方法:

<a href=".*?">([\s\S]*?)</a>

。。。或者这个:

preg_match('/\s*([^>]+)\s*<\/a/',$string,$match);
preg_match('/\s*([^>]+)\s*…或此:

preg_match('/\s*([^>]+)\s*<\/a/',$string,$match);
preg_match('/\s*([^>]+)\s*