Drive value of file_get_contents but only what is in quotation marks

0

Follow the code below:

<?
    $url="url_aqui";
    $result = file_get_contents($url);
?>  

The variable $result returns the value exactly this way: document.write('11,90');

I need to get only the value that is between the quotation marks, in the case: 11,90

What condition could you use to do this?

Updated code:

$valor_mensal="url_monthly";
$valor_trimestral="url_quarterly";
$valor_semestral="url_semiannually";
$valor_anual="url_annually";

$b_mensal = file_get_contents($valor_mensal);
$b_trimestral = file_get_contents($valor_trimestral);
$b_semestral = file_get_contents($valor_semestral);
$b_anual = file_get_contents($valor_anual);

$primeiroCaractere_b_mensal = strpos($b_mensal, "'");
$primeiroCaractere_b_trimestral = strpos($b_trimestral, "'");
$primeiroCaractere_b_semestral = strpos($b_semestral, "'");
$primeiroCaractere_b_anual = strpos($b_anual, "'");

$basico_mensal = substr($b_mensal, $primeiroCaractere_b_mensal+1, -3);
$basico_trimestral = substr($b_trimestral, $primeiroCaractere_b_trimestral+1, -3);
$basico_semestral = substr($b_semestral, $primeiroCaractere_b_semestral+1, -3);
$basico_anual = substr($b_anual, $primeiroCaractere_b_anual+1, -3);

The URL is composed like this:

link link link link

    
asked by anonymous 12.06.2018 / 03:50

2 answers

2

Mix substr and strpos . It looks something like this:

<?

$valor_mensal=processaValor("https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=monthly");
$valor_trimestral=processaValor("https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=quarterly");
$valor_semestral=processaValor("https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=semiannually");
$valor_anual=processaValor("https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=annually");

function processaValor($url) {
    $result = file_get_contents($url);
    $primeiroCaractere = strpos($result, "'");
    $valor = substr($result, $primeiroCaractere+1, -3);
    return valor;
}

?>

Value will receive the desired assignment.

    
12.06.2018 / 04:00
1

You can use preg_match to get the numeric value between the quotation marks:

$result = "document.write('11,90');";
preg_match('/(\.?\d,?)+/', $result, $match);
echo $match[0]; // imprime 11,90

Try RegExr

Explanation of regex:

()   -> captura o grupo
\.?  -> verifica se existe um ponto e captura também o que tiver antes de número (backreference)
\d   -> captura números
,?   -> verifica se há uma vírgula e captura números após
+    -> junta o que foi encontrado
    
12.06.2018 / 06:24