2013-09-03 5 views
3

나는이 어떻게 HTML, CSS를 제거하고 텍스트 만 얻을 수있는 등문자열에서 HTML 태그, CSS를 제거하는 방법은 무엇입니까?

<p> 
    <style type="text/css"> 
P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); }P.western { font-family: "Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "Arial Unicode MS",sans-serif; font-size: 12pt; }P.ctl { font-family: "Tahoma"; font-size: 12pt; } </style> 
</p> 
<p align="CENTER" class="western" style="margin-bottom: 0cm"> 
    <font size="5" style="font-size: 20pt"><u><b> TEXT I WANT TO GET </b></u></font></p> 

로 문자열?

메신저 strip_tags() 알고 있고, preg_replace와 함수를 쓸 수 있지만 거기 PHP를위한 작업 솔루션은 무엇입니까? 감사합니다. .

+1

DOM 파서를 사용할 수 있습니다. – elclanrs

+0

"PHP 용 작업 솔루션"이란 무엇입니까? strip_tags 및 preg_replace 정규식은 PHP에서 작동합니다. 코드에서 PHP 태그를 제거하고 싶습니까? 이 정규 표현식에 대해서조차도 괜찮을 수도 있습니다. –

+0

strip_tags 및 preg_replace를 알고 있습니다. 실제로 모든 HTML 태그와 모든 CSS 태그를 제거하고 깨끗한 텍스트를 제공하는 솔루션을 찾고있었습니다. – JTC

답변

8

사용 :

<?php 

$text = '<p> 
    <style type="text/css"> 
P { margin-bottom: 0.21cm; direction: ltr; color: rgb(0, 0, 0); }P.western { font-family: "Times New Roman",serif; font-size: 12pt; }P.cjk { font-family: "Arial Unicode MS",sans-serif; font-size: 12pt; }P.ctl { font-family: "Tahoma"; font-size: 12pt; } </style> 
</p> 
<p align="CENTER" class="western" style="margin-bottom: 0cm"> 
    <font size="5" style="font-size: 20pt"><u><b> TEXT I WANT TO GET </b></u></font></p>'; 

$text = strip_tags($text,"<style>"); 

$substring = substr($text,strpos($text,"<style"),strpos($text,"</style>")+2); 

$text = str_replace($substring,"",$text); 
$text = str_replace(array("\t","\r","\n"),"",$text); 
$text = trim($text); 

echo $text; 

?> 
+0

감사합니다. – VipinS

0

U 이것은 나를 위해 일한이

<?php 
$text = '<p>your string <p><!-- Comment --> <a href="#fragment">another strting </a><html>more text</html>'; 
echo strip_tags($text); 
?> 
0

모든 HTML 태그를 제거 할 수 있습니다.

함수 strip_tag_css ($ 텍스트) {

$text = strip_tags($text,"<style>"); 

$substring = substr($text,strpos($text,"<style"),strpos($text,"</style>")+2); 

$text = str_replace($substring,"",$text); 
$text = str_replace(array("\t","\r","\n"),"",$text); 
$text = trim($text); 

return $text; 

}

$ bodymensage = 않는 str_replace ('' ''의 html_entity_decode (strip_tag_css (strip_tags ($ 메시지)) ENT_QUOTES "UTF8"));

관련 문제