RegEx在HTML内容上不匹配

时间:2011-08-07 17:36:57

标签: php regex

有一个使用正则表达式的php程序,但在输出中没有显示任何东西! 怎么了?

<?php
$data='<div id="bodyContent" class="grid_16 push_4"> 

<form name="cart_quantity" action="http://iran-micro.com/product_info.php/products_id/1407/action/add_product" method="post"> 
<div> 
  <h1 style="float: left;">xxxxxxxxxxx</h1> 
  <h1>rrrrrrrrrrrrrrrr<br /><span class="smallText">zzzzzzzzzz</span></h1> 
</div> 
' ;

 preg_match('/<h1 style="float: left;">(?P<cost>.*?)</h1>.*?<h1>(?P<name>.*?)<br />/s',      $data, $matches);

echo $matches['name'];
echo $matches['cost'];
 ?>

1 个答案:

答案 0 :(得分:2)

如果您将其用作分隔符(您可以使用),则需要转义正则表达式中的/。如果不是“正则表达式部分”在第一个/停止,则会出现以下无效修饰符h。因此,您会收到以下警告:

Warning: preg_match(): Unknown modifier 'h' in test.php on line 11

对于使用html代码的正则表达式,我通常使用~作为分隔符,因为~在html代码中非常罕见(与/不同)。