php extracting first image from html string, remove all images tag, and truncate html string

心已入冬 提交于 2019-12-07 21:05:56

问题


I save in database an html content from tinymce editor, so I would want display this html content on php page, but in beautiful way. I would want first check if in html string is present an image, if it is present then display it first in php page. Then I would want display some text on right of image, truncating it first 200 words, but it must maintain html because it is possible that are present also links not only simple text.

Is there a php library or php class for manipulating html and for truncating html string for "read more" functionality?


回答1:


This looks like a nice tool:

http://simplehtmldom.sourceforge.net/

Funnily enough it even has the example your looking for on the front page.

EDIT:

PHP actually comes with its own library for handling HTML documented at: http://docs.php.net/manual/en/book.dom.php Also take a look at http://code.google.com/p/phpquery/



来源:https://stackoverflow.com/questions/12609063/php-extracting-first-image-from-html-string-remove-all-images-tag-and-truncate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!