2013-05-18 6 views
0

먼저 웹 페이지의 HTML을 가져오고 일반적으로 페이지 본문의 왼쪽 또는 오른쪽에 나타나는 href 링크를 제거합니다. Href 링크는 제거 중이지만 해당 레이블은 제거되지 않습니다.html 링크를 제거하고 html dom 파서를 사용하여 레이블을 지정하십시오.

예 :

<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a> 

링크가 제거되고 있지만 그것의 라벨 즉 '런던'. html 소스에서 전체 행을 제거하려면 어떻게해야합니까? 나는 그것을 위해 다음 코드를 사용하고 있습니다 :

은 당신이해야 할 모든는 erraser() 기능 두 다음 매개 변수 링크의 변수, 어떤 텍스트를 제공합니다 :

$string = strip_tags($html_source_code, '<a>', TRUE); 

function strip_tags($text, $tags = '', $invert = FALSE) { 
     preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
     $tags = array_unique($tags[1]); 
     if(is_array($tags) AND count($tags) > 0) { 
     if($invert == FALSE) { 
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
     } 
     else { 
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
     } 
     } 
     elseif($invert == FALSE) { 
     return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
     } 
return $text; 
} 
이제
+0

그럼,'return $ text;'는 무엇을 기대합니까? – samayo

답변

0

을 할 것입니다, 나는 치명적 얻을 오류 : strip_tags()를 다시 선언 할 수 없습니다.

이름 기능을 my_strip_tags와 같은 것으로 변경하면 문제가 없습니다.

function my_strip_tags($text, $tags = '', $invert = FALSE) { 
     preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
     $tags = array_unique($tags[1]); 
     if(is_array($tags) AND count($tags) > 0) { 
     if($invert == FALSE) { 
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
     } 
     else { 
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
     } 
     } 
     elseif($invert == FALSE) { 
     return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
     } 
return $text; 
} 

$html_source_code = "Beginning of content ... <a href='http://test.blogspot.com/2012/11/myblog.html'>London</a> ... end of content."; 

echo "<p>".$html_source_code."</p>"; 

$string = my_strip_tags($html_source_code, '<a>', TRUE); 

echo "<p>".$string."</p>"; 

그 인쇄 : 내용의

시작 ... London ... 내용의 끝.

콘텐츠의 시작 ... ... 콘텐츠의 끝.

+0

고맙습니다. 그것은 작동합니다. – Jerry3456

0
$link = "<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a>"; 

function erraser($theLink, $checkTag){ 

    if(strpos($theLink, $checkTag) == true){ 

     for($i=0; $i< strlen($theLink); $i++){ 
     $link[$i] = ''; 
     return $link[$i]; 
     } 
     }else{ 
     return $theLink; 
    } 

} 

이 볼 수 있습니다

예 : echo erraser($link, 'href');의 경우 링크가 삭제되고 return은 삭제됩니다. 당신은 그러나 다음 그것을 echo erraser($link, '----'); 내부 을 줄 경우, 링크 london, 의미를 줄 것이다, 그것은 링크인지 확인 여부를 내가 코드를 사용하는 경우 필요한 기능을