Stripping HTML tags in Magento
Asked Answered
I

3

5

This is probably pretty simple for most...

I have this line in Magento which is part of what posts to Pinterest.

<?php echo urlencode( $_product->getShortDescription() ) . " $" . urlencode( number_format( $_product->getPrice(),2 ) ); ?>

Somewhere in this, I need to strip tags as the short description uses a WYSIWYG editor and subsequently adds tags to database, I believe what I need to insert to the above is the following (as Magento has this function already):-

$this->stripTags

Please could anyone advise how this can be correctly added to the above without it breaking the page? Let me know if I need to supply anything further.

Thanks in advance.

Inductive answered 7/3, 2012 at 10:7 Comment(0)
C
11

This uses php's builtin function strip_tags and should work:

<?php echo urlencode( strip_tags($_product->getShortDescription()) ) . " $" . urlencode( number_format( $_product->getPrice(),2 ) ); ?>

To use Magento's function, use this:

<?php echo urlencode( $this->stripTags($_product->getShortDescription()) ) . " $" . urlencode( number_format( $_product->getPrice(),2 ) ); ?>

though this can only work if $this points to a valid object instance of "something" (sorry, I don't know Magento's internals)

Corazoncorban answered 7/3, 2012 at 10:17 Comment(1)
Excellent, thanks Hendrik. The Magento fix version still broke the page but using your first fix with php's strip_tags works perfectly.Inductive
F
1

With Magento 2, to remove HTML tags :

In a Block or .phtml file

Just use $this->stripTags($html).

In a Controller

    /**
     * @var \Magento\Framework\Filter\FilterManager
     */
    protected $filterManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Filter\FilterManager $filterManager
    ) {
        parent::__construct($context);
        $this->filterManager = $filterManager;
    }

    public function execute()
    {
        $html = "<ul>
                    <li>Foam-padded adjustable shoulder straps.</li>
                    <li>900D polyester.</li>
                    <li>Oversized zippers.</li>
                    <li>Locker loop.</li>
                </ul>";

        // Change thoses 2 variables if needed
        $allowableTags = null;
        $allowHtmlEntities = false;

        // Params are optionnal. If you use défault values you can remove it.
        $params = ['allowableTags' => $allowableTags, 'escape' => $allowHtmlEntities];

        $strWithoutHtml = $this->filterManager->stripTags($html, $params);

        die($strWithoutHtml); // Foam-padded adjustable shoulder straps. 900D polyester. Oversized zippers. Locker loop.
    }
Forcible answered 21/3, 2023 at 11:40 Comment(0)
B
0

Since stripTags function is available in all blocks and helpers you can just use

Mage::helper('core')->stripTags($data)
Blimey answered 19/2, 2018 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.