2010-01-23 33 views
1

我用下面的代碼進行修邊的標題,並顯示recent posts sectionblogPHP函數SUBSTR修剪問題

<?php global $post; 
$releaseDate = get_post_meta($post->ID, "gosterim_tarihi", true); 
foreach($images as $image) { 
    $title = get_the_title(); 
    if (strlen($title) > 20) { $title = substr($title, 0, 20) . '&hellip;'; } 
    $attachmentimage=wp_get_attachment_image_src($image->ID, 'large'); 
    echo '<li><a href="'. get_permalink() .'" title="' . $title . '"><img src="'. $attachmentimage[0] .'" alt="'. $title .'" />'. $title .'<span>Gösterim Tarihi: ' . $releaseDate . '</span></a></li>'; 
} ?> 

但也有HTML字符實體問題。當我使用substr函數修剪標題時,substr函數也修剪HTML字符實體。

所以我試圖使用html_entity_decode函數,但我不能做得很好。

任何人都可以幫到我嗎?

回答

0

我想你可以使用strip_tags功能有如:

substr(strip_tags($title), 0, 20); 

這將與標題不包括任何HTML字符只處理。

+0

不,它不起作用。我認爲'strip_tags'功能剝離了HTML標籤。但我想剝離HTML字符實體(http://htmlhelp.com/reference/html40/entities/)。 – fatihturan

3

試試這個:

$output = htmlentities(substr(html_entity_decode($input), 0, 20)); 

這將解碼所有實體,從而substr不會破壞任何東西。之後,您可以將所有字符編碼回其實體。

0

使用此功能

<?php 
    function keephtml($string){ 
      $res = htmlentities($string); 
      $res = str_replace("&lt;","<",$res); 
      $res = str_replace("&gt;",">",$res); 
      $res = str_replace("&quot;",'"',$res); 
      $res = str_replace("&amp;",'&',$res); 
      return $res; 
} 
?> 
0

這將是清潔的,如果你能留下的HTML編碼,直到最後一分鐘,例如當你實際回顯$標題字符串。當然,這意味着你必須記住要自己編碼所有的字符串。