PHP解析页面中的数据

PHP解析页面中的数据,php,Php,在我的脚本下,我试图获取被称为“UUID”的代码,但我得到了两个错误,如果您刚刚进入页面,调试将显示错误: 注意:第6行未定义索引:name 如果我单击按钮从值中获取信息,例如“TheChuckNorris007 Resident” 我得到一个错误: 致命错误:未捕获错误:调用n2k1.php:51堆栈跟踪中未定义的函数split():#0 n2k1.php(17):name2Key('TheChuckNorris0…'))#1{main}在第51行抛出 完整网址: 如果向下滚动,您将发现一个名

在我的脚本下,我试图获取被称为“UUID”的代码,但我得到了两个错误,如果您刚刚进入页面,调试将显示错误:

注意:第6行未定义索引:name

如果我单击按钮从值中获取信息,例如“TheChuckNorris007 Resident” 我得到一个错误:

致命错误:未捕获错误:调用n2k1.php:51堆栈跟踪中未定义的函数split():#0 n2k1.php(17):name2Key('TheChuckNorris0…'))#1{main}在第51行抛出

完整网址:

如果向下滚动,您将发现一个名为ChuckNorris007的h3

html:


我想重复一下粗体部分:*

我的Php文件:

<div align="center"><font size="-1"><font color="#000">
<?php
error_reporting(-1);
ini_set('display_errors', 'On');

$username = $_GET["name"];
if($username == "")
{
echo '<div align="center"><font size="-1">
<form action="n2k1.php" method="GET">
<input type="text" size="18" maxlength="40" value="TheChuckNorris007 Resident" name="name">
<br><input type="submit" value="lookup">
</form>';
}
else
{
$uuid = name2Key($username);
echo "<br>$uuid";
}
    function getPage($web)
    {
        $html = "";
          $ch = curl_init($web);
          curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
          curl_setopt($ch, CURLOPT_TIMEOUT, 5);
          $html = curl_exec($ch);
          if(curl_errno($ch))
          {
              $html = "";
          }
          curl_close ($ch);
        return $html;
    }
    function getBetween($content,$start,$end)
    {
        $a1 = strpos($content,$start);
        $content = substr($content,$a1 + strlen($start));
        while($a2 = strrpos($content,$end))
        {
            $content = substr($content,0,$a2);
        }
        return $content;
    }
    function name2Key($name)
    {
        $SL_SEARCH = 'http://search.secondlife.com/client_search.php?s=People&t=N&q=';
        $sName = split(' ',$name);
        $data = getPage($SL_SEARCH.$sName[0].'%20'.$sName[1]);
        $uuid = getBetween($data,'http://world.secondlife.com/resident/','"');
        if(!preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/",$uuid)) $uuid = '00000000-0000-0000-0000-000000000000';
        return $uuid;
    }
?>

您没有$_GET[“name”];在第一次装载时。
你必须像这样检查它:

$username = "";
if(isset( $_GET["name"])){
    $username = $_GET["name"];
}


第二个错误是什么:不要使用
split()
而是使用
explode()

我建议将处理逻辑和打印逻辑分开,这样可以提高代码的可读性。你也应该更加注意标签。不要使用
标记来应用样式——理想情况下,您需要尽可能少的标记。使用css设置文档的样式。在大多数情况下,如果要从有效的html文档中提取数据,那么应该使用DOM解析器来隔离所需的标记/属性值。一旦获得了目标文本,那么就应该推出正则表达式引擎

  • 关于
    $\u GET['name']
    错误。。。您需要检查它是否已声明,而不是长度为零的字符串。对此的直接检查是
    !empty()

  • split()
    不是函数。我甚至不能建议用
    explode()
    替换它。使用
    rawurlencode()
    会更直接。我个人的偏好是将数组传递给
    http\u build\u query()

  • 这是我对你剧本的调整。()


    split()
    不是函数。使用。可能您正在尝试使用str_split()?
    注意
    不是错误。感谢您的回复,我通过添加缺少的$\u GET并更改$sName=split(“”,$name)来消除错误;to$sName=爆炸(“”,$name);我的结果是00000000-0000-0000-0000-000000000000,而不是50374d97-060b-4b35-b1cc-4967dadca9c4,可能与爆炸部分有关。再次感谢您Dmytro。您可以使用var_dump($you_var)测试它是否包含您期望的内容。如果我的回答对你有用,请将其标记为对问题的回答或投票。谢谢
    $\u获取[“名称”]?:“”
    是一种三元检查,它假定变量已声明。我想你的意思是使用空合并操作符
    来检查它是否声明为非空。@mickmackusa是的,你是对的,我打错了。刚刚解决了这个问题。非常感谢您注意到这一点,并让我知道;)你认为
    ?:
    做什么?
    $username = "";
    if(isset( $_GET["name"])){
        $username = $_GET["name"];
    }
    
    $username = $_GET["name"]??"";
    
    <?php
    function getContent($web) {
        $ch = curl_init($web);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $html = curl_exec($ch);
        if (curl_errno($ch)) {
            $html = "";
        }
        curl_close ($ch);
        return $html;
    }
    
    function getUUID($name) {
        $html = getContent(
            'http://search.secondlife.com/client_search.php?'
            . http_build_query([
                's' => 'People',
                't' => 'N',
                'q' => $name
            ])
        );
    
        if ($html) {
            $dom = new DOMDocument; 
            $dom->loadHTML($html);
            $xpath = new DOMXPath($dom);
            foreach ($xpath->evaluate('//a[starts-with(@href, "https://world.secondlife.com/resident/")]') as $node) {
                if (preg_match("~https://world\.secondlife\.com/resident/\K[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}~", $node->getAttribute('href'), $m)) {
                    return $m[0];
                }
            }
            return '00000000-0000-0000-0000-000000000000';
        }
    }
    
    
    if (empty($_GET["name"])) {
        ?><div>
            <form action="n2k1.php" method="GET">
                <input type="text" size="18" maxlength="40" value="TheChuckNorris007 Resident" name="name">
                <br>
                <input type="submit" value="lookup">
            </form>
        </div><?php
    } else {
        echo getUUID($_GET["name"]);
    }