Heim  >  Artikel  >  Backend-Entwicklung  >  php的preg_match_all()匹配不到指定的字符串求解?

php的preg_match_all()匹配不到指定的字符串求解?

WBOY
WBOYOriginal
2016-09-29 09:33:001541Durchsuche

demo:

<code>$html='<div class="p-img"><a href="url"></a></div>
<div class="p-img"><img  src="xx" alt="php的preg_match_all()匹配不到指定的字符串求解?" ></div>';

preg_match_all('/<div class="p-img">(.*?)/', $html, $out);

var_dump($out);

<p>目标字符串$html,希望匹配</p>
<div class="p-img">,发现最后输出的$out是一直是empty,如何才能正确的匹配所有的结果,求正则高手指点
<hr>
<h3>找到答案之后的补充说明:</h3>
<p>利用爬虫从网络上获取到的dom结构,里面有很多的空格和换行,如果想把目标html匹配出来,正则表达式后面需要添加模式修正符号</p>
<pre class="brush:php;toolbar:false"><code>preg_match_all('/<div class="p-img">(.*?)/is', $html, $out);
<p>/i  忽略大小写</p>
<p>/s  将字符串视为单行,换行符作为普通字符;</p>

                            
                        


                                                                                                                        
                     <h2>回复内容:</h2>
                      
                                                            
<p>demo:</p>
<pre class="brush:php;toolbar:false"><code>$html='<div class="p-img"><a href="url"></a></div>
<div class="p-img"><img  src="xx" alt="php的preg_match_all()匹配不到指定的字符串求解?" ></div>';

preg_match_all('/<div class="p-img">(.*?)/', $html, $out);

var_dump($out);

<p>目标字符串$html,希望匹配</p>
<div class="p-img">,发现最后输出的$out是一直是empty,如何才能正确的匹配所有的结果,求正则高手指点
<hr>
<h3>找到答案之后的补充说明:</h3>
<p>利用爬虫从网络上获取到的dom结构,里面有很多的空格和换行,如果想把目标html匹配出来,正则表达式后面需要添加模式修正符号</p>
<pre class="brush:php;toolbar:false"><code>preg_match_all('/<div class="p-img">(.*?)/is', $html, $out);
<p>/i  忽略大小写</p>
<p>/s  将字符串视为单行,换行符作为普通字符;</p>

                            
                        
            <p class="answer fmt" data-id="1020000007037958">
                                    
</p>
<pre class="brush:php;toolbar:false"><code>$html='<div class="p-img"><a href="%5C'url%5C'"></a></div>
<div class="p-img"><img  src="%5C'xx%5C'" alt="php的preg_match_all()匹配不到指定的字符串求解?" ></div>';

preg_match_all('/<div>(.*?)/is', $html, $out);

var_dump($out);
<p>输出:</p>
<pre class="brush:php;toolbar:false"><code>array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(39) "<div class="p-img"><a href="url"></a></div>"
    [1]=>
    string(39) "<div class="p-img"><img  src="xx" alt="php的preg_match_all()匹配不到指定的字符串求解?" ></div>"
  }
  [1]=>
  array(2) {
    [0]=>
    string(14) "<a href="url">"
    [1]=>
    string(14) "<img  src="xx" alt="php的preg_match_all()匹配不到指定的字符串求解?" >"
  }
}</a></code>

要我怎么说呢,你的 $html 里面的 \/ 应该 /,并且 ' 没有转义为 \'

我运行通不过,你知道么,是你语法问题,还是正则的问题?

答案在$html定义哪里。

-号需要转义

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:请问那这个XHR请求为什么在开发者选项里没有Reply XHR菜单呢?Nächster Artikel:php做反向代理,如何把所有请求都指向index.SegmentFault

In Verbindung stehende Artikel

Mehr sehen