2014-05-21 37 views
-2

現在我修改這個代碼使用PHP來刪除 在meta描述(Magento的)

<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" /> 

是這個代碼

<meta name="description" content="<?php echo strip_tags($this->getDescription()) ?>" /> 

,所有可怕的HTML已經從我的剝離元描述如果保留爲空,則從描述中自動生成。任何想法如何我可以修改此刪除&nbsp;並將其替換爲一個空格?

+3

你嘗試過使用字符串替換找到'' 與'' – secretformula

+0

http://stackoverflow.com/questions/15289772/php-convert-html-更換nbsp-to-space-gt-to-etc – Prashank

+0

['str_replace(' ','',$ this-> getDescription());'](http://www.php.net/manual/en/function .str-replace.php) – Sam

回答

1

str_replace('&nbsp;', ' ', $this->getDescription())將返回另一個字符串,用&nbsp;替換爲空格。這意味着你可以只需更換$this->getDescription()(返回一個字符串)與str_replace()功能:

<meta name="description" content="<?php echo strip_tags(str_replace('&nbsp;', ' ', $this->getDescription())) ?>" /> 

這可能是一個少許清潔劑,如果你打破它:

<?php 
$description = $this->getDescription(); 
$description = strip_tags($description); 
$description = str_replace('&nbsp;', ' ', $description); 
?> 

<meta name="description" content="<?=$description?>" /> 

<?=$description?>shorthand<?php echo $description; ?>

1

我認爲正確的做法是修復源數據。您可以在Magento後端執行此操作,或直接在數據庫中更新數據(db表catalog_product_entity_text,您需要從eav_attribute數據庫表中找到正確的attribute_id)。

0

看看@How to remove html special chars?

$description = preg_replace("/&#?[a-z0-9]+;/i","", $description); 
+0

@MahonriMoriancumer你的觀點是什麼?我的回答只是一個鏈接嗎? –