Php regular expression for extracting table content

Source: Internet
Author: User
Find a Php regular expression string to extract table content, which may be as follows: $ s1 & nbsp; here is the description character (& amp; nbsp;) & lt; table & nbsp; nameoptionsTable & nbsp; cellpadding0 & nbsp; cellspacing0 & nbsp; & gt; & lt; tr & gt; & lt; td & find a regular expression for Php to extract table content
The string may be as follows:
$ S1 = 'here is the description character ()
AAAAA BBBBB
';

$ S2 = 'here is the description character ()
0
';

We hope to extract the character before the table: Here is the character string between the description character () 'and td.
'Bbbbbb', 'aaaaa', '', '0'
The content in table td may be complex. consider various situations as much as possible.


Do you know the description is clear? Please advise!

------ Solution ----------------------
$ S1 = 'here is the description character ()
 
AAAAA BBBBB
';
$ S2 = 'here is the description character ()
0
';
// Match the content before the table
Preg_match_all ('/(. + ?) ] +?> /I ', $ s1, $ p1 );
Echo"
";
print_r($p1[1]);
echo "
";
// Match the content in td
Preg_match_all ('/ ] +?> (. + ?) <\/Td>/I ', $ s2, $ m2 );
Echo"
";
print_r($m2[1]);
echo "
";

// $ P1 [1] and $ m2 [1] are matched content

------ Solution ----------------------


$ S1 = 'here is the description character ()
AAAAA BBBBB
';

$ S2 = 'here is the description character ()
0
';

// Change your mind. do not extract but delete it.
// Add two more table/tr tags.
$ Replace = array ('/<\ s * td. *>/U','/<\ s * \/td \ s *>/u ');

Echo preg_replace ($ replace, '', $ s1 );
Echo'
';
Echo preg_replace ($ replace, '', $ s2 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.