Jump to content

Help with str_pad


weezle

Recommended Posts

Hi!

 

I'm a total beginner with php, but I want to solve certain problem on my website.

 

I have this piece of code:

if( empty($product_name) && $product_parent_id!=0 ) $product_name = $dbp->f("product_name");
$product_html = str_replace( '{product_name}', shopMakeHtmlSafe( $product_name ), $product_html );

 

I'm working with $product_name, I wanted to limit the character length that are being displayed on web site. So I used substr function and I got this:

 

if( empty($product_name) && $product_parent_id!=0 ) $product_name = $dbp->f("product_name");
$product_html = str_replace( '{product_name}', shopMakeHtmlSafe( $product_name = substr($product_name,0,), $product_html );

 

And now I want to add a condition:

if a string is longer than 8 characters, add ... (three dots) at the end

if a string is longer shorter 8 characters, don't add nothing

 

I know I must use str_pad function, but at this level it's just too much for me, I can't figured it out how to apply it in this piece of code.

 

Can anybody help me?

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/143798-help-with-str_pad/
Share on other sites

mmm no, you are looking at the wrong function.

 

$product_html = str_replace( '{product_name}', shopMakeHtmlSafe(substr($product_name,0,), $product_html );
$product_html = (strlen($product_html) >  ? $product_html . "..." : $product_html;

 

Should get you what you want. That is called, concatenation. The ? and : are the Ternary operators which work like a IF/ELSE statement, just shortened.

Link to comment
https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754488
Share on other sites

Thanks for helping premiso but it's now working.

 

Instead of shrinked product name followed by ... I get this:

 

{product_name}

...

 

FYI, I'm want to modify Joomla/VirtueMart module, but it is pure php issue, so I'm not getting much help from those communities.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754536
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.