2017-01-23 71 views
-1

我想仅使用PHP,没有jquery包含页面,如果div包含单词YES。这样做的原因是我可以使用CMS将div内的内容从YES更改为NO以包含页面。我尝试过preg_match,但它在PHP本身中搜索变量或精确短语。这就是我要找:PHP如果div包含单词包括页面

<div id="available">YES</div> 

<?php if (preg_match("/YES/i", ID="available")) { 
include 'page.php'; 
} else { 
echo "Not Available."; 
} 
?> 

我需要的ID =“可用”搜索DIV ID =“可用” YES时被发现,包括页面,如果没有,回声“不可用。 “谢谢你的帮助。

+0

'如果一个div包含单词YES'在哪里?在同一个PHP文件或字符串?这个div标签位于哪里? –

+0

div是一个page1.php,如果page1.php上id =“available”的div包含单词YES,我想包含page2.php。 – AlwaysLearning

+0

问题中的代码将全部位于同一页面上,page1.php – AlwaysLearning

回答

0

我想通了。这将允许您使用cushycms编辑您的PHP变量以包含页面。如果div id =“available”是除Yes之外的任何内容,page2.php将不会包含在page1.php中。代码附在下面。

page1.php中

<h1>This is Page1.php </h1> 
<?php 
// locate page you want to check div on - page2.php 
$content = file_get_contents('page2.php'); 
// input your specific div id from - page2.php 
$first_step = explode('<div id="available">' , $content); 
$second_step = explode("</div>" , $first_step[1]); 
// name your variable - in this case - $available 
$available = trim($second_step[0]); 
// input your output variable - $available and word to match in div - 'Yes' 
if(false !== stripos($available, 'Yes')){ 
// if word in div=id'available' on page2.php is Yes - include page or echo 
include 'page2.php'; 
} 
?> 

注:使page2.php,这和cushycms工作,你必须把DIV ID =“可用”的另一个DIV中有类或样式 - 参见下面的例子

使page2.php

<div id="wrapper" class="cushycms"> 
<div id="available">Yes</div> 
</div> 
<h2>Please include this rental into the page</h2>