2017-01-23 46 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>