views:

420

answers:

6

Hi,

I'm using below code for triming titles and show it recent posts section on my blog:

<?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>';
} ?>

But there are problem with HTML character entities. When i use substr function for trim a title, substr function trimming HTML character entities too.

So i tried to use html_entity_decode function but i can't do it very well.

Anyone can help me?

A: 

I think you can use the strip_tags function there eg:

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

This will deal only with the title excluding any html chars.

Sarfraz
No, it doesn't work. I think `strip_tags` function is stripping HTML tags. But i want to strip HTML character entities (http://htmlhelp.com/reference/html40/entities/).
fatihturan
+2  A: 

Try this:

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

This will decode all entities so substr won't break anything. After that you can encode all characters back to their entities.

Tomas Markauskas
A: 

try this

or use strip_tags then use substr

moustafa
A: 

Use This Function

<?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;
}
?>
streetparade
A: 

It would be cleaner if you can leave the html-encoding until the last minute, e.g. when you actually echo the $title string. Of course, that means you have to remember to encode all the strings yourself.

JW
+1  A: 

Use mb_substr for multibyte character encodings like UTF-8. substr just counts bytes while mb_substr counts characters.

substr() works with singlebyte only http://php.net/manual/en/function.mb-substr.php

Dan