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 ()
';
$ S2 = 'here is the description character ()
';
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 ()
';
$ S2 = 'here is the description character ()
';
// 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 ()
';
$ S2 = 'here is the description character ()
';
// 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 );