2011-08-01 7 views
1

그래서 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 느낌 여름과 풍미 오늘, 우리는 그것을했다 인정해야 이 모든 전채, 모든 디저트 또는 모든 음료를 만들기위한 충동에 저항하는 제비 ... 자세히보기

답변

2

HTML 태그는 총 문자 수에 포함됩니다. 다음 미리 샘플을 먼저 태그를 스트립 :

$preview = strip_tags($posts[$counter]['body']); 
$posts[$counter]['preview'] = substr($preview, 0, 150).'...'; 

또한, 하나는 보통 계속 나타 내기 위해 잘린 텍스트의 끝 부분에 타원을 ("...")에 추가합니다.

<p><br>과 같이 원하는 태그를 제거 할 때 잠재적 인 단점이 있습니다. XML 스타일 태그 (<br />) 다음을 던질 수있는 미리 양해,

$preview = strip_tags($posts[$counter]['body'], '<br><p>'); 
$posts[$counter]['preview'] = substr($preview, 0, 150).'...'; 

을하지만 : 당신이 그를 유지하려면, 당신은 strip_tags에 대한 두 번째 인수로 전달할 수 있습니다. 혼합 된 XML/HTML을 다루는 경우 htmLawed과 같은 태그 필터링을 사용해야하지만 개념은 동일하게 유지됩니다. 잘라내 기 전에 HTML을 제거하십시오.

+0

ah 예 .. body 태그의 태그를 제거하는 것을 완전히 잊었습니다. 감사! – CodeCrack

1

<regular-body> 태그를 보면 HTML이 포함 된 것으로 보입니다. 따라서 DOMDocument (http://www.php.net/manual/en/domdocument.loadhtml.php)로 구문 분석하는 것이 좋습니다. 그런 다음 모든 항목을 반복하고 특정 태그를 무시할 수 있습니다 (예 : <img> 무시하고 <p> 유지). 그런 다음 원하는 것을 렌더링하고 150 자까지자를 수 있습니다.