English 中文(简体)
我如何显示 php 变量中存储的 html 代码
原标题:how do I display html code stored within a variable in php
  • 时间:2012-05-25 13:18:18
  •  标签:
  • php
  • html
  • echo

在我 index.html 页面内,我从用户那里获取一个嵌入链接,例如:

<iframe width= 560  height= 315  src= http://www.youtube.com/embed/GasAmUfvXJs      frameborder= 0  allowfullscreen></iframe>

此代码将传递到我的 PHP 脚本, 它会将它保存在变量中, 并对它进行一些验证 。 在验证成功之后, 我试图回溯 PHP 脚本中的变量 。 但是它没有正确显示它 。

以下是 PHP 脚本 :

<?php

if(isset($_POST[ embed ]) && isset($_POST[ date ])){

$embed = $_POST[ embed ];
$date = $_POST[ date ];

if(!empty($embed) && !empty($date)){

    echo "OK - VIDEO";
    $final = $embed;
    echo "$final";

}else{
    $final = "Try again - not empty";
    echo "$final";
}

}else{

echo "try again - not set";

} 

?>
问题回答

如果您对 html 进行回声, 浏览器就会提供 html, 如果您想要对 html 的来源进行回声, 那么您就会使用 : < a href=" http://php. net/ manual/ en/ office. htmlentities. php" rel = “ nofollow” >htmlentites ()

echo htmlentites($final);

如果不是,请解释: 但是它没有正确显示它。

其它: 您不应该输出 ANY html 一个用户输入或您的脚本被设计打开到 XSS, 验证不是 ! noty( $embed) ! 您应该将视频的 Youtube id从代码中提取出来, 并从中建立您自己的嵌入代码 。

如何做到这一点 :

<?php 
if($_SERVER[ REQUEST_METHOD ]== POST ){
    if(!empty($_POST[ embed ]) && !empty($_POST[ date ])){

        if(preg_match( #http://www.youtube.com/embed/([a-zA-Z0-9_-]+)"# ,$_POST[ embed ],$match)==true){

            $embed = <<<EMBEDCODE
<iframe width="640" height="360"
        src="http://www.youtube.com/embed/{$match[1]}?feature=player_embedded"
        frameborder="0" allowfullscreen>
</iframe>
EMBEDCODE;

        }else{
            $embed = "Not a real youtube embedcode! Try Again";
        }

    }else{
        $embed = "Enter youtube embedcode.";
    }
}
?>

<h1>Youtube Yada</h1>
<form method="POST" action="">
  <p>Embedcode:<textarea rows="9" name="embed" cols="44"></textarea></p>
  <p>Date:<input type="text" name="date" value="<?php echo date("F j, Y, g:i a");?>" size="20"></p>
  <p><input type="submit" value="Submit" name="B1"></p>
</form>

<?php echo(isset($embed)?$embed:null);?>

测试时:

<iframe width="560" height="315" src="http://www.youtube.com/embed/GasAmUfvXJs" frameborder="0" allowfullscreen></iframe>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签