Home > Backend Development > PHP Tutorial > 正则小问题

正则小问题

WBOY
Release: 2016-06-23 14:17:55
Original
1207 people have browsed it

本帖最后由 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>
Copy after login

$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"  }}
Copy after login

少了〈/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];
Copy after login

<?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];
Copy after login
Copy after login

<?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];
Copy after login
Copy after login
膜拜大神!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template