There are several ways to get the desired one, but I'll stick to answers that meet the difficulty posed in the question.
The value is picked up by the class="numbers positive" element
PHP
if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" ))
{
echo "Erro ao abrir a página de indices" ;
exit;
}
$conteudo = '';
while(!feof($fp))
{
$conteudo .= fgets($fp,1024);
}
fclose($fp);
$valorCompraHTML = explode('class="numbers">', $conteudo);
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa = trim($ibovespa[0]);
$valorMais = explode('class="numbers positivo">', $conteudo);
$mais = trim(strip_tags($valorMais[1]));
$mais = explode(' ', $mais);
$maisResult = trim($mais[0]);
HTML
R$ <?php echo $bovespa ?> <?php echo $maisResult ?>
Another way to get the same result
As stated in the author's question O campo sem explodir está assim: 74.294 +0,99
does not match reality, in fact the field returns like this,
Sowemustremoveallextraspacesbeforetheexplodetobethus74.294+0,99
if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" ))
{
echo "Erro ao abrir a página de indices" ;
exit;
}
$conteudo = '';
while(!feof($fp))
{
$conteudo .= fgets($fp,1024);
}
fclose($fp);
$valorCompraHTML = explode('class="numbers">', $conteudo);
// Esta é a variável que eu preciso explodir
$campo5 = trim(strip_tags($valorCompraHTML[5]));
//Estes são os valores HTML para exibir no site.
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
//retira todos os espaços em branco e retorna 74.294 +0,99
$ibovespa = preg_replace(array("/\t/", "/\s{2,}/", "/\n/", "/\r/"), array("", " ", " ", " "), $ibovespa);
$ibovespa = explode(' ', $ibovespa);
$bovespa = trim($ibovespa[0]);
$mais = trim($ibovespa[1]);
script for Dollar, euro etc ...
Explode () was created with the intention of separating a string into an array of several smaller strings. For this it uses certain characters, passed by parameters, to do the separation.
In programming, gambiarra is a palliative (and creative) way of solving a problem or correcting a system in an inefficient, inelegant or incomprehensible way, but it still works.
This is a code that prints the repetition of the phrase "Hello world!" 5 times using this for a loop to control
#include <stdio.h>
int main (void) {
int i;
for (i=0; i<5; i++) {
printf("Hello world!");
}
return 0;
}
This is a code that prints on the screen the repetition of the phrase "Hello world" 5 times, but using gambiarra to obtain the final result:
#include <stdio.h>
int main (void) {
printf("Hello world!");
printf("Hello world!");
printf("Hello world!");
printf("Hello world!");
printf("Hello world!");
return 0;
}