0

Hi I've created a system that adds articles in the database, the user can embed youtube or dailymotion or simple flash into the text area. In the home page, I've inserted a slideshow that slides new feed with image, I've writen a simple condition if that checks if the text contains an embeded video using:

if (ereg('<object>',$text){}

I just want to insert the video(object element in general) in the slide show in case there's an object in the text. in other words I want to extract what's between <object> and </object>

thank you

SmootQ
  • 2,096
  • 7
  • 33
  • 58
  • Bare in mind that `ereg` is deprecated as of PHP 5.3.0. http://us2.php.net/manual/en/function.ereg.php – ncuesta Dec 20 '10 at 21:38
  • http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Marc B Dec 20 '10 at 21:49
  • @ncuesta but what does this mean if it's deprecated of PHP 5.3.0 – SmootQ Dec 20 '10 at 22:04
  • 1
    it means that in future releases of PHP the function will be removed, and your code won't be valid anymore. In this particular case, it is adviced to drop `ereg()` for `preg_match()`: http://php.net/manual/en/migration53.deprecated.php – ncuesta Dec 20 '10 at 22:06
  • @ncuesta , thank you my friend , I will remove it from my code +1 for your comment – SmootQ Dec 21 '10 at 00:02

1 Answers1

3

I'm not sure about the slideshow part or if you are also trying to modify the content. But for the extraction part there's two options. You can use a simple-minded regular expression like:

preg_match("#<object[^>]*>(.+?)</object>#ims", $text, $matches);
print $matches[1];

Or a more reliable HTML parser with a readable API like phpQuery or QueryPath:

print qp($text)->find("object")->text();

The latter would also allow you to extract attributes more easily.

mario
  • 144,265
  • 20
  • 237
  • 291