[PHP] Is there a good way to extract the / content in HTML with/without closing tag?

Sun, 23 May 2010 02:28:33 -0700


I want to extract all contents started with <embed> and <object>
with/without closing tags.
My solution is using a regular expression to get it work, but there is some
exception I could not handle out.

The REGEXs I used are:

// With closing tag
if (preg_match_all("#(<(object|embed)[^>]+>.*?</\\2>)#is", $str,
$matchObjs)) {
  // blahblah

// Without closing tag
} else if (preg_match_all("#(<(?:object|embed)[^>]+>)#",$str,$matchObjs)){
  // blahblah

But it might be failed if the $str are mixed with/without closing tags:

$str ='<div><div><object type="application/x-shockwave-flash"><param
name="zz" value="xx"></object></div><div><embed src="http://sample.com";

In this situation, it will only get the
<object type="application/x-shockwave-flash"><param name="zz"

but I want to get the two results which are
<object type="application/x-shockwave-flash"><param name="zz"
<embed src="http://sample.com"; />

So, is there a good way to use one REGEX to process this issue?

Thanks for your help.

- Chi-An Hsieh

Reply via email to