그래서 XML 파일을 구문 분석하고 READ MORE 링크로 기사의 처음 150 단어를 표시하려고합니다. 그것은 정확하게 150 단어를 구문 분석하지 않습니다. IMG 태그 코드 등을 파싱하지 않도록하는 방법을 잘 모르겠습니다. 코드가 다음과 같습니다.XML/HTML 문자열 길이 제한
// Script displays 3 most recent blog posts from blog.pinchit.com (blog..pinchit.com/api/read)
// The entries on homepage show the first 150 words of description and "READ MORE" link
// PART 1 - PARSING
// if it was a JSON file
// $string=file_get_contents("http://blog.pinchit.com/api/read");
// $json_a=json_decode($string,true);
// var_export($json_a);
// XML Parsing
$file = "http://blog.pinchit.com/api/read";
$posts_to_display = 3;
$posts = array();
// get all the file nodes
if(!$xml=simplexml_load_file($file)){
trigger_error('Error reading XML file',E_USER_ERROR);
}
// counter for posts member array
$counter = 0;
// Accessing elements within an XML document that contain characters not permitted under PHP's naming convention
// (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
foreach($xml->posts->post as $post){
//post's title
$posts[$counter]['title'] = $post->{'regular-title'};
// post's full body
$posts[$counter]['body'] = $post->{'regular-body'};
// post's body's first 150 words
//for some reason, I am not sure if it's exactly 150
$posts[$counter]['preview'] = substr($posts[$counter]['body'], 0, 150);
//strip all the html tags so it doesn't mess up the page
$posts[$counter]['preview'] = strip_tags($posts[$counter]['preview']);
//post's id
$posts[$counter]['id'] = $post->attributes()->id;
$posts_to_display--;
$counter++;
//exit the for loop after we parse out all the articles that we want
if ($posts_to_display == 0) break;
}
// Displays all of the posts
foreach($posts as $post){
echo "<b>" . $post['title'] . "</b>";
echo "<br/>";
echo $post['preview'];
echo " <a href='http://blog.pinchit.com/post/" . $post[id] . "'>Read More</a>";
echo "<br/><br/>";
}
결과는 다음과 같습니다.
에디터의 피크 : 클럽 Sportiva 아무것도 당신이 아니라 완전히 무료로 세련되고 정교한, 섹시한 스포츠카의 휠 뒤에 날이 제어 느낌도하지 않습니다. 호텔 유타 살롱 호텔 유타 자세히보기
월요일 메뉴 : 그것은 더
Pinchy 음료 & 바위를 읽고 놀랄 없다 매운 자몽, 파프리카, Creamsicles 느낌 여름과 풍미 오늘, 우리는 그것을했다 인정해야 이 모든 전채, 모든 디저트 또는 모든 음료를 만들기위한 충동에 저항하는 제비 ... 자세히보기
ah 예 .. body 태그의 태그를 제거하는 것을 완전히 잊었습니다. 감사! – CodeCrack