开发者

How to extract an object tag from a text

开发者 https://www.devze.com 2023-01-31 17:57 出处:网络
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

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 t开发者_JS百科hat 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消