如何根据来自textarea字段的file_get_contents显示多个结果?

时间:2017-11-02 17:00:03

标签: javascript php

我正在尝试构建一个多文本域搜索器,但我最终只得到一个结果。我有一个textarea字段,您可以在其中添加多个域和一个文本字段,您可以在其中输入文本。 例如,我有多个域并将其粘贴到textarea字段

abc.com/dog.txt  
def.com/cat.txt
ghi.com/mice.txt
jkl.com/bug.txt
然后我把一个关键词' cute'在文本字段中,结果应为

record found on abc.com    
record found on def.com    
record not found on  ghi.com    
record not found on jkl.com

的search.php     

    <label>URL</label><br/>
    <textarea rows="4" cols="10" name="domainlist" id="domainlist" placeholder="Add Your Domain here seperated by comma"></textarea><br/>
    <label>Keyword</label><br/>
    <input type="text" name="keyword" id="keyword" placeholder="Keyword " />
    <input type="submit">    

</form>

Result.php

$domainlist = htmlspecialchars($_POST['domainlist']);
$keyword = $_POST["keyword"];   

$file = file_get_contents('http://' .$domainlist); 
$searchnum = $keyword ; 

if (stripos($file, $searchnum) !== false) { 


    echo 'record found on'   .$domainlist;


} 

else {


    echo 'record not found' .$domainlist ;
}

如何显示多个结果?

1 个答案:

答案 0 :(得分:0)

$domainlist = array(
    abc.com/dog.txt  
    def.com/cat.txt
    ghi.com/mice.txt
    jkl.com/bug.txt
);
$keyword = $_POST["keyword"]; 
foreach($domainlist as $domain) {
    $file = file_get_contents('http://' . $domain);
    $searchnum = $keyword ; 

    if (stripos($file, $searchnum) !== false) { 
        echo 'record found on'   .$domain;
    } 
    else {
        echo 'record not found' .$domain;
    }
}

如果您的$domainlist需要来自浏览器(通过POST),那么您需要使用多个具有相同名称的元素将其作为数组传递:

<input name="domainlist[]" value="abc.com/dog.txt" />
<input name="domainlist[]" value="def.com/cat.txt" />

还有其他方法可以使用javascript或某种形式的序列化或json对象来处理$domainlist