简体   繁体   English

如何在php变量中存储HTML

[英]How to store HTML in php variable

what I would like to do is store the HTML of a site in a php variable so it can be echoed at a certain condition. 我想做的是将网站的HTML存储在php变量中,以便可以在特定条件下回显它。 My PHP code: 我的PHP代码:

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
$website1 = ?
$website2 = ?
if($url == $URL) {
    echo $website1;
} else if($url == $URL2) {
    echo $website2;
}
?>

I want this HTML (below) to be stored in $website1 我希望此HTML(如下)存储在$website1
There will also be HTML stored in $website2 $website2还将存储HTML

<html>
    <head>
        <link rel='stylesheet' type='text/css' media='all' href='css/style.css'>
        <link rel='stylesheet' type='text/css' media='all' href='css/jquery-ui-1.10.4.custom.css'>
        <title>Website #One</title>
        <script type='text/javascript' src='js/jquery-1.9.1.js'></script>
        <script type='text/javascript' src='js/jquery-ui-1.10.4.custom.js'></script>
        <script type='text/javascript' src='js/script.js'></script>
    </head>
    <body>
    <div id='header'></div>
    <div id='menu'>
        <button id='navbtn' class='active btn'>Home</button><button id='navbtn2' class='btn'>Pictures</button><button id='navbtn3' class='btn'>Videos</button><button id='navbtn4' class='btn'>Games</button><button id='navbtn4' class='btn'>About</button><button id='navbtn5' class='btn'>Contact</button>
    </div>
    <div id='content'></div>




    </body>
</html>

How would I be able to achieve this? 我将如何实现这一目标?

I don't understand what logic you are using, but you can achieve this using the PHP's built in function file_get_contents() . 我不明白您使用的是什么逻辑,但是您可以使用PHP的内置函数file_get_contents()实现此目的。 Use this way: 使用这种方式:

<?php
    $url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
    $URL = "http://www.example.com/";
    $URL2 = "http://www.example2.com/";
    $website1 = file_get_contents($URL);
    $website2 = file_get_contents($URL2);
    if($url == $URL) {
        echo $website1;
    } else if($url == $URL2) {
        echo $website2;
    }
?>

You can use file_get_contents(); 您可以使用file_get_contents(); Or the CURL library http://php.net/manual/fr/book.curl.php 或CURL库http://php.net/manual/fr/book.curl.php

Curl is a nice way to do this: http://www.digimantra.com/technology/php/get-data-from-a-url-using-curl-php/ Curl是执行此操作的好方法: http : //www.digimantra.com/technology/php/get-data-from-a-url-using-curl-php/

<?php
$url='http://twitter.com/statuses/user_timeline/16387631.json'; //rss link for the twitter timeline
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to

/* gets the data from a URL */

function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

Thanks guys I was able to learn from your answers and managed to get it to work here is my final php code: 谢谢大家,我能够从您的答案中学习并设法使它在这里工作,这是我最终的php代码:

<?php
$get_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = "http://thedag.ga/thedagga/main.php";
$url2 = "http://thedag.ga/zp/zp18.php";
$tdurl1 = "http://thedag.ga/";
$tdurl2 = "http://www.thedag.ga/";
$zpurl1 = "http://zp.thedag.ga/";
$zpurl2 = "http://zp18.thedag.ga/";
$zpurl3 = "http://zp4rker.thedag.ga/";
if($get_url == $tdurl1 || $get_url == $tdurl2) {
    print_r(file_get_contents($url1));
} else if($get_url == $zpurl1 || $get_url == $zpurl2 || $get_url == $zpurl3) {
    print_r(file_get_contents($url2));
}
?>

Why not just put both versions in there own PHP file, that is, if it is only some simple one page html, and not an entire website, then the other suggestions would probably be better. 为什么不将两个版本都放在自己的PHP文件中,也就是说,如果它只是一些简单的一页html,而不是整个网站,那么其他建议可能会更好。

<?php
$url = "http://".getenv("HTTP_HOST")."/".getenv("REQUEST_URI");
$URL = "http://www.example.com/";
$URL2 = "http://www.example2.com/";
if($url == $URL) {
    include 'header1.php';
} else if($url == $URL2) {
    include 'header2.php';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM