-2

Right now I've modified this code

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

to be this code

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

and all the horrible html has been stripped from my meta descriptions that are auto generated from the description if left empty. Any idea how I could modify this to remove &nbsp; and replace it with a space as well?

5
  • 3
    have you tried using string replace to find &nbsp; and replace with ` ` Commented May 21, 2014 at 20:03
  • stackoverflow.com/questions/15289772/… Commented May 21, 2014 at 20:05
  • str_replace('&nbsp;', ' ', $this->getDescription()); Commented May 21, 2014 at 20:06
  • @secretformula Not yet. I'm not sure how to do that exactly without looking it up (I'm just starting to learn PHP) and I'm not sure how to incorporate it safely into that without breaking it to be honest. Commented May 21, 2014 at 20:06
  • @Sam How would I do that combined with strip_tags? Commented May 21, 2014 at 20:07

3 Answers 3

1

str_replace('&nbsp;', ' ', $this->getDescription()) will return another string with &nbsp; replaced with a space. This means you can just replace $this->getDescription() (which returns a string) with the str_replace() function:

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

It could be a little cleaner if you break it up:

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

Note <?=$description?> is shorthand for <?php echo $description; ?>.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the correct approach is to fix the source data. You can do that in the Magento backend or update data directly in the database (db table catalog_product_entity_text, you need to find out correct attribute_id from eav_attribute db table).

Comments

0

Take a look @ How to remove html special chars?

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

1 Comment

@MahonriMoriancumer what is your point? Is my answer a link only?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.