热搜:NVER node 开发 php

正则小问题

2024-09-20 10:00:01
正则小问题

本帖最后由 snipersheep 于 2013-08-01 15:52:47 编辑

$str = '内容A 内容B'
请问一下,用正则分别取出 内容A 跟 内容B 正则要怎样写呢?
注:内容A,内容B 中间会有换行符。

回复讨论(解决方案)

你要得到的事内容A和内容B呢,还是 id="test_0"的text和 id="test_1"的内容呢?

内容A 与 内容B
跟ID无关。不是JS。是PHP取数据。

$subject = ' 				you dead!				hello				james			            				shit!				hi				
  • list
';$pattern = '/这里正则如何写/';preg_match($pattern, $subject, $matches);print_r($matches);$pattern_1 = '/这里正则如何写/';preg_match($pattern_1, $subject, $matches);print_r($matches);分别取到结果如下:数据一:you dead!hellojames数据二:shit!hi
  • list

$str = '内容A内容B';
preg_match_all('/(.*?)<\/div>/i',$str,$match);
echo $match[2][0];//内容A
echo $match[2][1];//内容B

默默收藏

preg_match_all('/(.*?)<\/div>/is',$subject,$match);
var_dump($match);

preg_match_all('/(.*?)<\/div>/is',$subject,$match);
var_dump($match);

好像有点问题。不是很准。

array(3) {  [0]=>  array(2) {    [0]=>    string(86) " 				you dead!				hello"    [1]=>    string(80) "				shit!				hi"  }  [1]=>  array(2) {    [0]=>    string(12) " id="test_0""    [1]=>    string(12) " id="test_1""  }  [2]=>  array(2) {    [0]=>    string(45) " 				you dead!				hello"    [1]=>    string(39) "				shit!				hi"  }}

少了〈/div〉

preg_match_all('/(\s*(?:\s*)*)<\/div>/us',$subject,$match);echo $match[0][0];echo '
';echo $match[0][1];

<?php$subject = '                 you dead!                hello                james                                        shit!                hi                
  • list
';preg_match_all('/]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '
';echo $match[0][1];

<?php$subject = '                 you dead!                hello                james                                        shit!                hi                
  • list
';preg_match_all('/]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '
';echo $match[0][1];
膜拜大神!