League\CommonMark\HtmlRenderer::escape PHP Method

escape() public method

public escape ( string $string, boolean $preserveEntities = false ) : string
$string string
$preserveEntities boolean
return string
    public function escape($string, $preserveEntities = false)
    {
        if ($preserveEntities) {
            $string = preg_replace('/[&](?![#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)/i', '&', $string);
        } else {
            $string = str_replace('&', '&', $string);
        }
        return str_replace(['<', '>', '"'], ['&lt;', '&gt;', '&quot;'], $string);
    }

Usage Example

 /**
  * @param FencedCode $block
  * @param HtmlRenderer $htmlRenderer
  * @param bool $inTightList
  *
  * @return HtmlElement
  */
 public function render(AbstractBlock $block, HtmlRenderer $htmlRenderer, $inTightList = false)
 {
     if (!$block instanceof FencedCode) {
         throw new \InvalidArgumentException('Incompatible block type: ' . get_class($block));
     }
     $infoWords = $block->getInfoWords();
     $attr = count($infoWords) === 0 || strlen($infoWords[0]) === 0 ? array() : array('class' => 'language-' . $htmlRenderer->escape($infoWords[0], true));
     return new HtmlElement('pre', array(), new HtmlElement('code', $attr, $htmlRenderer->escape($block->getStringContent())));
 }
All Usage Examples Of League\CommonMark\HtmlRenderer::escape