About the use of regular expressions to read the segment of an article & lt; strong & gt; $ title = html_entity_decode ($ arrArticle ['title']); $ subtitle = html_entity_decode ($ arrArticle ['subtitle']); $ sourc questions about using regular expressions to read article segments
$ Title = html_entity_decode ($ arrArticle ['title']);
$ Subtitle = html_entity_decode ($ arrArticle ['subtitle']);
$ Source = html_entity_decode ($ arrArticle ['source']);
$ Author = html_entity_decode ($ arrArticle ['author']);
$ Intro = html_entity_decode ($ arrArticle ['Intro']);
$ Body = $ arrArticle ['content'];
$ Text = eregi_replace ('
',"
", $ Body );
$ Text = eregi_replace (" ] *>
"," ", $ Text );
$ Text = eregi_replace (" ] *)>
"," ", $ Text );
$ Body = stripslashes (str_replace ("
","
", $ Text ));
$ Body_arr = explode ("
", $ Body );
$ Body1 = $ body2 = $ body3 = '';
$ Total = count ($ body_arr );
$ Maxed = max (floor ($ total/2), 3 );
Foreach ($ body_arr as $ k => $ v ){
If ($ k = 0 ){
$ Body1 = $ v ."
";
} Else if ($ k <$ maxed ){
$ Body2. = $ v ."
";
} Else {
$ Body3. = $ v ."
";
}
}
This code matches the regular expression.
Section 1
Section 2
... To read the number of segments. However, there is a situation where some texts are segmented as follows:
Section 1
Section 2
Section 3
How can I write code for these two situations?
------ Solution --------------------
Discussion
PHP code
$ S ='
Section 1
Ss
Section 2
Section 3
';
$ Ar = preg_split ('/[<\/? P>
]/', $ S,-1, PREG_SPLIT_NO_EMPTY );
Print_r ($ ar );
PHP code
Array
(
[0] => First paragraph
[1] => ss
[2] => Second paragraph
[3 ......