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*