限制PHP Magento Instant Search的单词数量

时间:2013-06-21 04:42:53

标签: php string magento limit

我正在尝试限制在页面上运行时显示的单词数量。我在某些地方使用了一个小脚本,但在这种情况下我似乎无法获得任何工作。这是代码的和平:

    }
    $html .=" <button type='button' class='button-search-buy' onclick='javascript:document.productAddToCartForm.submit()'><span>". $product_helper->__(' ') ."</span></button>";
    $html .="</fieldset>";
}


$html .="<div id='description'><h4 style='margin:5px 0 3px 0'><span style='color:#e26703'>".Mage::helper("catalog/product")->__("Quick Overview")."</span>&nbsp;&nbsp;<a style='font-size:10px' href='".$product_url."'  target='_self'>".$product_helper->__('View Detail')."</a></h4>"
            . $product->getShortDescription() <-------Right here            
      ."</div>"     
."</div>
  </div></form>";
$this->getResponse()->setHeader('Content-type', 'application/x-json');
 $this->getResponse()->setBody($html);      

以下是我发现的一些代码可行,但我无法想象如何使用上面这个开放式php代码。

<?php $sdesc2 = $_product->getShortDescription();
$sdesc2 = trim($sdesc2);
$limit = 120;
    if (strlen($sdesc2) > $limit) {
      $sdesc2 = substr($sdesc2, 0, strrpos(substr($sdesc2, 0, $limit), ' '));
    } ?>
<?php echo $sdesc2."..."; ?>     

1 个答案:

答案 0 :(得分:0)

<?php $sdesc2 = $product->getShortDescription();
$sdesc2 = trim($sdesc2);
$limit = 120;
    if (strlen($sdesc2) > $limit) {
      $sdesc2 = substr($sdesc2, 0, strrpos(substr($sdesc2, 0, $limit), ' '));
    } ?>

$html .="<div id='description'><h4 style='margin:5px 0 3px 0'><span style='color:#e26703'>".Mage::helper("catalog/product")->__("Quick Overview")."</span>&nbsp;&nbsp;<a style='font-size:10px' href='".$product_url."'  target='_self'>".$product_helper->__('View Detail')."</a></h4>"
        . $sdesc2 ."</div>"