PHP显示图像在htmlspecialchars异常中

时间:2015-02-07 09:40:34

标签: php html

我有PHP函数来显示所有HTML代码。

示例:

<?php echo htmlspecialchars("<p>Welcome</p>"); ?>

然后结果将<p>Welcome</p>

现在我需要显示图片<img src="http://nl3.php.net/images/logo.php"/>,但它也会显示代码<img src="http://nl3.php.net/images/logo.php"/>

如何显示图像?

2 个答案:

答案 0 :(得分:1)

代码描述

代码将在文本中找到每个图像标签<img attr1=value1 attr2=value2 ... />,为每个图像创建带有new HtmlElement()的图像标签-此类只是new DOMElement()的自定义精简版本,将原始图像标签替换为占位符,在将每个图像都替换为占位符之后,在这种情况下,我选择此占位符{#@#IMG_' . $id .'},对于我的文本类型而言,该占位符足够原始,然后将其用于整个文本htmlspecialchars()函数,最后它将用新创建的图像标签替换占位符,然后返回文本。

代码说明

if (preg_match_all('#<img\s+([^>]+?)\s?(/>|>)#i', $text, $matches)) {检查文本是否包含任何图像,如果是,则变量$matches将包含3维数组数组,例如:

array (3)
   0 => array (6)
   |  0 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   |  1 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   |  2 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   |  3 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   |  4 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   |  5 => "<img alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes" />"
   1 => array (6)
   |  0 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   |  1 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   |  2 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   |  3 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   |  4 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   |  5 => "alt="rolleyes" src="//t.domain.com/default/rolleyes.gif" title="rolleyes"" 
   2 => array (6)
   |  0 => "/>"
   |  1 => "/>"
   |  2 => "/>"
   |  3 => "/>"
   |  4 => "/>"
   |  5 => "/>"

然后根据原始<img />属性创建自己的图像,并将其存储在变量$images

foreach ($matches[1] as $id => $imgAttributes) {
    $img = new \HtmlElement('img');
    $img->addAttributes($imgAttributes);
    $img->attributes([
        'src' => $img->attr('src'),
        'alt' => escapeHtml($img->attr('alt')),
        'title' => escapeHtml($img->attr('title'))
    ]);
    $images[$id] = $img->render();

就我而言,我只需要图像中的属性alttitlesrc,如果您不需要在alt,{ {1}}或其他代码,您可以省略这部分代码,并在下一行代码中稍作改动:

title

它将用占位符$text = str_replace($matches[0][$id], '{#@#IMG_' . $id .'}', $text); 替换每个图像标签的原始文本,因此,例如,第一个图像将替换为原始文本,例如:

'{#@#IMG_' . $id .'}'

请记住,占位符对于您的文本类型应足够原始,并且不能包含任何特殊字符,对于HTML实体,这些特殊字符将被{#@#IMG_0} 替换。

当每个图像都被自己的占位符替换时,我们可以将特殊字符转换为整个文本中的HTML实体:

htmlspecialchars()

最后一步,我们用图像标签替换文本中的占位符:

$text = escapeHtml($text);

整个代码

foreach ($images as $id => $image) {
    $text = str_replace('{#@#IMG_' . $id . '}', $image, $text);
}

您不需要处理属性function escapeHtml($text) { return htmlspecialchars($text, ENT_QUOTES, 'UTF-8', false); } function escapeHtmlKeepImages($text) { if (preg_match_all('#<img\s+([^>]+?)\s?(/>|>)#i', $text, $matches)) { $images = []; foreach ($matches[1] as $id => $imgAttributes) { $img = new \HtmlElement('img'); $img->addAttributes($imgAttributes); $img->attributes([ 'src' => $img->attr('src'), 'alt' => escapeHtml($img->attr('alt')), 'title' => escapeHtml($img->attr('title')) ]); $images[$id] = $img->render(); $text = str_replace($matches[0][$id], '{#@#IMG_' . $id .'}', $text); } $text = escapeHtml($text); foreach ($images as $id => $image) { $text = str_replace('{#@#IMG_' . $id . '}', $image, $text); } } else { $text = escapeHtml($text); } return $text; } alt等的版本,例如

title

输出:


$text = '
<strong>hello</strong>
<img src="image.gif" alt="something" >><3
how are you?
<img src="signature.gif" alt="signature"  title="signature"/>
';

function  escapeHtml($text)
{
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8', false);
}
function escapeHtmlKeepImages($text)
{
    if (preg_match_all('#<img\s+([^>]+?)\s?(/>|>)#i', $text, $matches)) {
        array_map(
            function ($id, $img) use (&$text) {
                $text = str_replace($img, '{#@#IMG_' . $id . '}', $text);
            },
            array_keys($matches[0]), $matches[0]
        );

        $text = escapeHtml($text);

        array_map(
            function ($id, $img) use (&$text) {
                $text = str_replace('{#@#IMG_' . $id . '}', $img, $text);
            },
            array_keys($matches[0]), $matches[0]
        );
    } else {
        $text = escapeHtml($text);
    }
    return $text;

}

echo escapeHtmlKeepImages($text);

答案 1 :(得分:-1)

我也是PHP和HTML的初学者。 让我试着解决这个问题。

我们可以看到“欢迎”的结果,您可以改为

    <p><?php echo htmlspecialchars("Welcome"); ?></p>

它只会显示“欢迎”。我们可以使用相同的方法来处理as

    <img <?php echo htmlspecialchars("src='http://nl3.php.net/images/logo.php");?>/> 

虽然它不是一个好的编程风格,但您可以先解决问题,然后再学习preg_match()。干杯!