I have a problem with a PHP breadcrumb function I am using, when the page name is very long, it overflows out of the box, which then looks really ugly.
My question is, how can I achieve this: “This is a very long string” to “This is…” with PHP?
Any other ideas on how I could handle this problem would also be appreciated, thanx in advance!
Here is the breadcrumb function:
function breadcrumbs() {
// Breadcrumb navigation
if (is_page() && !is_front_page() || is_single() || is_category()) {
echo '<ul class="breadcrumbs">';
echo '<li class="front_page"><a href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a> <span style="color: #FFF;">»</span> </li>';
if (is_page()) {
$ancestors = get_post_ancestors($post);
if ($ancestors) {
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $crumb) {
echo '<li><a href="'.get_permalink($crumb).'">'.get_the_title($crumb).'</a> <span style="color: #FFF;">»</span> </li>';
}
}
}
if (is_single()) {
$category = get_the_category();
echo '<li><a href="'.get_category_link($category[0]->cat_ID).'">'.$category[0]->cat_name.'</a></li>';
}
if (is_category()) {
$category = get_the_category();
echo '<li>'.$category[0]->cat_name.'</li>';
}
// Current page
if (is_page() || is_single()) {
echo '<li class="current">'.get_the_title().'</li>';
}
echo '</ul>';
} elseif (is_front_page()) {
// Front page
echo '<ul class="breadcrumbs">';
echo '<li class="front_page"><a href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a></li>';
echo '<li class="current">Home Page</li>';
echo '</ul>';
}
}
If you want a more nice (word limited) trucation you can use
explode
to split the string by spaces and then append each word (array entry) until you’ve reached your max limitSomething like:
If you are working with UTF-8 as charset, I suggest using the
mb_strimwidth
method as it is multibyte safe and won´t mess up multibyte chars. It also appends a placeholder string like...
automatically, withsubstr
you´d have to do that in an additional step.Usage sample:
You can safely use substr.
and eventually wordwrap() to break long words
$string = “This is a very long string”;
$newString = substr( $string, 0, 7).”…”;
// Output = This is…
Ideally, it should be done on the client side. You can use CSS/JS for the same.
Set this CSS property: text-overflow: ellipsis.
However, it will work only in IE. To use the same in Firefox as well, you can do something like this.
If you do not mind javascript plugins, use one of the jQuery ellipsis plugin.
Edit: These methods will work even when dealing with unicode, which can be a bit tricky if you try to handle this using php. (Like substr function)
Edit 2: If your problem is just the overflowing text and you do not mind not having the “…” at the end then it is even more simple. Simply, use the CSS:
text-overflow: hidden;
.You can truncate the string at max length and then search for the last space:
Multibyte safe (Requires PHP > = 4.2)
Following is not multibyte safe
You’re after a truncate function. This is what I use:
And usage: