2017-05-23 6 views
1

텍스트는 HTML 형식 단락 태그로 저장됩니다.MySQL에서 html 형식으로 저장된 데이터의 경우 PHP에서 하위 문자열을 사용하는 방법?

필드 이름 : DESCR

저장된 데이터

:

enter image description here

내가 PIN이 필요 : 테이블에 저장 구조에만 datas 위

<html> 
<head> 
    <title></title> 
</head> 
<body> 
<div class="campusInfoNamesake"> 
<p>If someone had said to me ten years ago that autism and the therapeutic benefits of gardening will become your life, I wouldn&rsquo;t have believed them.</p> 

<p>Although it&rsquo;s still very early days, things are starting to take shape on my smallholding in Ceredigion, West Wales.</p> 
</div> 
</body> 
</html> 

가 울부 짖는 소리 MySQL의 테이블 이미지를 볼 수 본문 태그에만 100 문자열. 나는이 c를 사용했다. ode : <?php echo substr($row01[descr], 100); ?> 이 코드를 사용하면 결과를 얻을 수 없습니다. 도와주세요.

+0

$ row01 [descr] 데이터가 있는지 확인하십시오. –

답변

0

<body> 태그 사이의 모든 것을 검색하려면 정규식을 사용해야합니다. 그런 다음 필요한 문자열을자를 수 있습니다. 여기

preg_match_all('/<body>(.*?)<\/body>/s', $row01['desc'], $matches); 

//HTML array in $matches[1] 
print_r(substr($matches[1],100)); 

참조 정규식 : PHP Regex find text between custom added HTML Tags

0

나는 신발에 자신을 넣어 가장이 텍스트를 준비하는 방법에 대한 몇 가지 가정을 만들려고하고 있습니다. ... 내가 여기에 잘못된 방향으로 갈거야 알려줘

입력 :

$row01['descr']='<html> 
<head> 
    <title></title> 
</head> 
<body> 
<div class="campusInfoNamesake"> 
<p>If someone had said to me ten years ago that autism and the therapeutic benefits of gardening will become your life, I wouldn&rsquo;t have believed them.</p> 

<p>Although it&rsquo;s still very early days, things are starting to take shape on my smallholding in Ceredigion, West Wales.</p> 
</div> 
</body> 
</html>'; 

방법 (preg_match pattern demo) (php demo) :

$body_text=preg_match('/<body>\s\K.+(?=<\/body)/s',$row01['descr'],$out)?$out[0]:''; // extract body text 
$tagless_text=strip_tags($body_text);      // remove tags from body text 
$decoded_text=html_entity_decode($tagless_text);   // decode entities like: &rsquo; 
$pattern=['/[\n\r ]+/','/^ +| +$/'];        
$replace=[' ','']; 
$compacted_text=preg_replace($pattern,$replace,$decoded_text); // remove extra spacing 
$truncated_text=substr($compacted_text,0,100);  // cut the string to max length of 100 
echo $truncated_text,'...';       // print to screen and append with ... 

출력 :

If someone had said to me ten years ago that autism and the therapeutic benefits of gardening will b...