正则小问题

WBOY
풀어 주다: 2016-06-23 14:17:55
원래의
1152명이 탐색했습니다.

本帖最后由 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 = '<div class="superStar" id="test_0"> 				you dead!				<div class="show">hello</div>				<span>james</span>			</div>            <div class="superStar" id="test_1">				shit!				<div class="contne">hi</div>				<ul><li>list</li></ul>            </div>';$pattern = '/这里正则如何写/';preg_match($pattern, $subject, $matches);print_r($matches);$pattern_1 = '/这里正则如何写/';preg_match($pattern_1, $subject, $matches);print_r($matches);分别取到结果如下:数据一:you dead!<div class="show">hello</div><span>james</span>数据二:shit!<div class="contne">hi</div><ul><li>list</li></ul>
로그인 후 복사

$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) "<div class="superStar" id="test_0"> 				you dead!				<div class="show">hello</div>"    [1]=>    string(80) "<div class="superStar" id="test_1">				shit!				<div class="contne">hi</div>"  }  [1]=>  array(2) {    [0]=>    string(12) " id="test_0""    [1]=>    string(12) " id="test_1""  }  [2]=>  array(2) {    [0]=>    string(45) " 				you dead!				<div class="show">hello"    [1]=>    string(39) "				shit!				<div class="contne">hi"  }}
로그인 후 복사

少了〈/div〉

preg_match_all('/<div class=\"superStar\"(.*?)>(\s*(?:<div.*<\/div>\s*)*)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];
로그인 후 복사

<?php$subject = '<div class="superStar" id="test_0">                 you dead!                <div class="show">hello</div>                <span>james</span>            </div>            <div class="superStar" id="test_1">                shit!                <div class="contne">hi</div>                <ul><li>list</li></ul>            </div>';preg_match_all('/<div[^>]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];
로그인 후 복사
로그인 후 복사

<?php$subject = '<div class="superStar" id="test_0">                 you dead!                <div class="show">hello</div>                <span>james</span>            </div>            <div class="superStar" id="test_1">                shit!                <div class="contne">hi</div>                <ul><li>list</li></ul>            </div>';preg_match_all('/<div[^>]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];
로그인 후 복사
로그인 후 복사
膜拜大神!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!