weezle Posted February 4, 2009 Share Posted February 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143798-help-with-str_pad/ Share on other sites More sharing options...
premiso Posted February 4, 2009 Share Posted February 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754488 Share on other sites More sharing options...
weezle Posted February 4, 2009 Author Share Posted February 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754536 Share on other sites More sharing options...
premiso Posted February 4, 2009 Share Posted February 4, 2009 You must have a line break before the end. Try this: $product_html = (strlen($product_html) > ? str_replace("\n", "...\n", $product_html) : $product_html; And see if that fixes it. Quote Link to comment https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754553 Share on other sites More sharing options...
weezle Posted February 4, 2009 Author Share Posted February 4, 2009 Now it's like this: ... {product_name} The problem is that a real product name from database is not being displayed. Quote Link to comment https://forums.phpfreaks.com/topic/143798-help-with-str_pad/#findComment-754568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.