Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改php,使其仅在元描述字段为空时运行,并提取2个句子(Magento)_Php_Magento - Fatal编程技术网

修改php,使其仅在元描述字段为空时运行,并提取2个句子(Magento)

修改php,使其仅在元描述字段为空时运行,并提取2个句子(Magento),php,magento,Php,Magento,我在Magento中有以下PHP代码,到目前为止,它采用了描述的第一句话,并将其作为元描述 <?php if (Mage::registry('current_product')) : ?> <?php if (strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('cur

我在Magento中有以下PHP代码,到目前为止,它采用了描述的第一句话,并将其作为元描述

<?php if (Mage::registry('current_product')) : ?>
<?php if (strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1)))=="") : ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.strip_tags(str_replace("<br />",", ",substr(Mage::registry('current_product')->getDescription(), 0, strpos(Mage::registry('current_product')->getDescription(), '.')+1))).'" />' ?>
<?php endif; ?>
<?php else: ?>
<?php echo '<meta name="description" content="'.htmlspecialchars($this->getDescription()).'" />' ?>
<?php endif; ?>

我试着做两件事,修改它,使其仅在产品缺少手动设置的元描述时运行,并使其提取前2句而不是前1句。
不可能

是的,不可能。因为,如果产品描述是空的,你根本无法从中提取两句话。一个也没有;)

注:

  • 您不需要在每一行上都使用PHP标记
  • 您的if语法有效,但并不常见
  • 仅从注册表获取一次描述,而不是4次
  • 比较为空,以测试是否设置了描述
  • 如果未设置,则打印“无说明”
  • 如果设置了,则得到两句话
  • 为了获取两个句子,我添加了函数triser(),它来自某处
--


您不必以任何行开头,请将代码缩进一点哦,对不起,我的意思是如果元描述字段为空。比如,如果我在meta description字段中放了一些东西,使用它,如果我没有在meta description字段中放一些东西,那么从描述中提取句子并去除特殊字符。我相应地更新了代码。请看一看。如果为产品设置了元描述,它将自动设置。如果没有元描述,则提取产品描述,并将前两句话拆分、剥离并插入元标记中作为描述。
<?php
if (Mage::registry('current_product'))
{
    $metadescription = $this->description();        

    if ($metadescription === '') {
        $description = Mage::registry('current_product')->getDescription();
        $twoSentences = teaser($description, 2);

        echo '<meta name="description" content="'.htmlspecialchars($twoSentences).'" />';
    }
}

function teaser($body, $sentencesToDisplay = 2)
{
    $nakedBody = preg_replace('/\s+/',' ',strip_tags($body));
    $sentences = preg_split('/(\.|\?|\!)(\s)/',$nakedBody);

    if (count($sentences) <= $sentencesToDisplay)
        return $nakedBody;

    $stopAt = 0;
    foreach ($sentences as $i => $sentence) {
        $stopAt += strlen($sentence);

        if ($i >= $sentencesToDisplay - 1)
            break;
    }

    $stopAt += ($sentencesToDisplay * 2);

    return trim(substr($nakedBody, 0, $stopAt));
}