Motrar value of file_get_contents but only what is in quotes

Asked

Viewed 74 times

0

Follows code:

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

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

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

What condition could I 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 this way:

https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=monthly https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=quarterly https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=semiannually https://dominio.com/feeds/productsinfo.php?pid=3&get=price&billingcycle=annually

2 answers

2


Mix replace and strpos. It goes 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.

  • What if the value is 111.90 or 1.111.90? It will not always be the same value there.

  • The last parameter, when negative, will always count from the end of the string. Then it removes the last three characters, which will always be the same ');

  • Very good, it worked.. But it was very extensive code, can you help me simplify the code if possible? Because there are several plans I will need to make. I updated the question with the updated code.

  • You can create a function that will return this value. I will update the answer

  • Thank you very much. I added more details, how is the URL.

1

Can use preg_match to pick the numerical value between the quotation marks:

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

Testing in the Regexr

Explanation of the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.