How can I download and display first 200 or 250 characters from a web-page?
I have a list of urls for which I want to display first 200 or 250 characters. Can I do it using jquery or should I download them on the server side [using PHP] and store them in database?
I guess I will have to use fopen with limit of characters. **
Edit
First 200 characters of "body" excluding tag开发者_运维技巧s. Like summary
Reading your title, my first inclination is to use FOPEN, but there are a few things that came to mind...
1) Are there "new lines" in your target HTML code? For example, if you look at the source code of google.com, the whole "page" is only 15 lines of code. Hence, that would not work.
2) Do you need to take into account formatting? Something as simple as a font tag or a link could take up most (or all) of the 200 character limit.
You may want to look into:
strip_tags(..)
http://php.net/manual/en/function.strip-tags.php
How I would do it...
FOpen the page and store to string then strip_tags(..) the string and substr(..) the string "buffer".
Hope this helps.
You could do it with the simple html dom parser. This is kind of slow, though. So you might consider storing the page contents in a database if you are displaying many excerpts on one page.
<?php
include("simple_html_dom.php");
$html = file_get_html("http://www.stackoverflow.com");
echo substr(str_replace(" ", "", $html->plaintext), 0, 200);
?>
精彩评论