简体   繁体   English

PHP解析内容为输入->名称值

[英]php parse content for input->name value

I'm trying to parse html content, for the value stored in the name tag. 我正在尝试解析html内容,以获得存储在名称标签中的值。

My current try looks like the following: 我目前的尝试如下所示:

function getAuthToken(){
    $html = file_get_contents('url');
    $values = array();

    foreach($html->find('input') as $element)
    {
        $values[$element->name] = $element->value;
    }

    print_r($values);
}

What am I doing wrong? 我究竟做错了什么?

$html in your code is not an object its a string (the results of file_get_contents). 您代码中的$ html不是一个对象,它是一个字符串(file_get_contents的结果)。

$object->name is a class object $ object-> name是一个类对象

$array[key] is an array element $ array [key]是一个数组元素

Neither of these in your code will do what you want to achieve with the result of file_get_contents because your $html var is a string, you need to do some string parsing to get the desired results. 您的代码中的这两个都不会通过file_get_contents的结果来实现您想要的结果,因为您的$ html var是一个字符串,您需要进行一些字符串解析才能获得所需的结果。

You can check that with: 您可以使用以下方法进行检查:

echo gettype($html);

You can also just echo out $html to find out what the string looks like to give you a better idea of what you are working with, for instance, is there a common character that you can explode the sting with an so getting an array to work with within your foreach. 您也可以回显$ html来找出字符串的样子,以便更好地了解所使用的字符串,例如,是否存在可以用数组炸开字符串的常见字符,从而获得数组与您的foreach合作。

example: 例:

$newarray = explode('&', $html);

foreach ($newarray as $key => $value) {
 //do your thing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM