You are here:  » Strrpos function

Support Forum



Strrpos function

Submitted by Harvey on Sat, 2008-12-20 13:28 in

Hi David,

A while back I had a problem where every product on a feed had a 6 digit number on the end (but I had to keep it on to keep products seperate/unique), so you gave me this code to get rid of it on the fly:

<?php
 
print substr($product["name"],0,strrpos($product["name"]," ")); 
?>

I want to keep this functionality, but also get rid of anything after the first 20 characters and have a "...". Could you tell me how to do this without ruining the above code?

Many thanks!

Submitted by support on Sat, 2008-12-20 18:58

Hi Harvey,

Sure - try this:

<?php
 
print substr(substr($product["name"],0,strrpos($product["name"]," ")),0,20)."...";
?>

...or a slightly more complicated version that will only display the ... if it is longer than 20 characters:

<?php
 $pn 
substr($product["name"],0,strrpos($product["name"]," "));
 if (
strlen($pn)) > 20$pn .= "...";
 print 
$pn;
?>

Cheers,
David.