Get the value of an input by name

1

Hello,

I have the following function:

function capturar($string, $start, $end) {
    $str = explode($start, $string);
    $str = explode($end, $str[1]);
    return $str[0];
}

I use it with cURL, to get the value of an input.

Example usage:

$pagina = curl("http://example.com");
$texto = capturar($pagina, 'name="texto"', '"');
echo $texto;

In that case, PHP would return the value of the input or any tag that has name of texto , so far so good, but the page that cURL accesses has the following input:

<input value="João Lima" name="texto" id="texto"/>

How would I get the input value in this order?

Thank you in advance.

Current code I'm using:

<?php
function curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd().'/test.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd().'/test.txt');
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$array = [];
preg_match('/value="([^"]+)"/e', curl('http://example.com'), $array);
var_dump($array);
echo '<br><br><br><br>'.curl('http://example.com');

On this page you have other inputs, but you have an input with value of 11.5, 12, 12.5 .

    
asked by anonymous 07.01.2017 / 21:35

1 answer

0

If it's a single input, this should resolve:

$conteudo = '<input value="João Lima" name="texto" id="texto"/>';
$array = [];
preg_match('/value="([^"]+)"/e',$conteudo,$array);
var_dump($array);

This is just an example, it should be adapted. $conteudo should be the return of your request made with CURL.

    
07.01.2017 / 21:46