Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析单词之间有空格的关键字_Php - Fatal编程技术网

Php 解析单词之间有空格的关键字

Php 解析单词之间有空格的关键字,php,Php,我正在从一个网站获取数据,当我解析“数学、化学、科学”等单个单词时,下面提到的脚本工作正常。然而,如果我试图解析一个包含空格的关键字,如“商业数学”等,浏览器只会永远加载,它似乎不起作用。请引导我 <?php include("simple_html_dom.php"); $keywords = "business math,chemistry,science"; $keywords = explode(',', $keywords); foreach($keywords as $ke

我正在从一个网站获取数据,当我解析“数学、化学、科学”等单个单词时,下面提到的脚本工作正常。然而,如果我试图解析一个包含空格的关键字,如“商业数学”等,浏览器只会永远加载,它似乎不起作用。请引导我

<?php
include("simple_html_dom.php");

$keywords = "business math,chemistry,science";
$keywords = explode(',', $keywords);

foreach($keywords as $keyword) {
    echo '<br><b><font color="red">Keyword: </font><font color="blue">'.$keyword.'</font></b><br>';

    $html = file_get_html('http://www.tutorvista.com/search/'.$keyword);

    $i = 1;
    foreach($html->find('div[style=padding:20px; border-top:thin solid #DDDDDD; border-bottom:none;]') as $element) {
        foreach($element->find('div[class=entry-abstract]') as $div) {
            $title[$i] = $div->plaintext.'<br><br>';
        }
        $i++;
    }
    print_r($title);
}
?>

问题在于:

$html = file_get_html('http://www.tutorvista.com/search/'.$keyword);
该函数在内部使用file_get_contents(),该文件不接受空格,需要使用urlencode()对URI进行编码

试试这个:

$html = file_get_html( urlencode('http://www.tutorvista.com/search/'.$keyword) );
参考: