PHP如果div包含word include页面

时间:2017-01-23 04:09:16

标签: php

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

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

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

我需要ID =“available”来搜索div id =“available”,如果找到YES,则包括页面,如果没有,则回显“Not Available”。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。这将允许您使用cushycms编辑PHP变量以包含页面。如果div id =&#34;可用&#34;除了是之外,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上为此工作,使用了ACTIVEycms,你必须把div id =&#34;可用&#34;在另一个div里面有类或样式 - 见下面的例子

使page2.php

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