回声未转义的HTML

时间:2019-03-23 03:03:56

标签: php

非常不寻常的问题。

几年来,我遇到了一些代码,其中包含条件和正常的PHP语法,同时回显了所有内容。

我的问题是该技术/语法是如何调用的。我一直在搜索一些非常宽泛的术语,找不到我正在寻找的东西。

如果我的记忆正确,那么我很久以前查看的代码未转义HTML,并且不需要使用<?php ?>

开始和停止PHP处理

我在名为Template\Labels::User()的类中有一个方法 该方法的唯一目的是回显正确的html,以在我的Web应用程序中创建标签,从而使页面的代码变浅,并且对查看该代码的任何人都清晰可见。

我想避免,我不得不<?php ?>boolean if

有人知道我在找什么吗?

static function User($UserObj,$isLink = true){
    ?>
    <div class="image label bg-purple" style="margin: 4px;">
        <?php if($isLink){
            ?><a href=""><?php
        } ?>
        <img src="<?php echo $UserObj -> ProfilePicture; ?>" style="height: 2em;" class="img-circle" alt="User Image">
        <label style="font-size: 90%"><?php echo $UserObj->FirstName{0}.$UserObj->LastName{0}; ?></label>
        <?php if($isLink){
            ?></a><?php
        } ?>
    </div>
    <?php
}

已编辑

通过对Operator的PHP文档进行更多研究之后 我发现Nowdoc字符串引用了

有人可以向Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML透露一些信息吗

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

2 个答案:

答案 0 :(得分:1)

最好在您的问题中添加代码,以便我们在这里都能看到您正在处理的内容。现在对我来说,我对您的问题的理解是,您希望避免使用php标签根据if条件回显一些html代码。

<?php 
    static function User($UserObj,$isLink = true){
        $html = '<div class="image label bg-purple" style="margin: 4px;">';
        if($isLink) $html .= '<a href="">';
        $html .= '<img src="'.@$UserObj->ProfilePicture.'" style="height: 2em;" class="img-circle" alt="User Image">';
        $html .= '<label style="font-size: 90%">'.@$UserObj->FirstName[0].@$UserObj->LastName[0].'</label>';
        if($isLink) $html .= '</a>';
        echo $html;
    }
?>

在我看来,我认为您只需要运行php标签一次,并使用一个简单的变量向其中添加html代码,以便可以在函数末尾进行打印。

答案 1 :(得分:0)

我听不懂您的某些图片,但同样的问题是在PHP中打印未转义的html。换句话说,您想要原始的html。

目前正在考虑使用两个函数,您可以根据所需的输出使用它们: html_entity_decode() htmlentities()

html_entity_decode() htmlentities()相反,它可以将字符串中的所有HTML实体转换为适用的字符。

<?php     $orig = "I'll \"walk\" the <b>d
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll &quot;walk&quot
echo $b; // I'll "walk" the <b>
?>

参考:http://www.php.net/html_entity_decode

我希望这有助于解决您的未转义html问题。

相关问题