There are several ways to get the desired, but I will stick to answers that meet the difficulty postulated in the question.
The value is taken 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 correspond to reality, in fact the field returns like this,
so we should take out all the extra spaces before the explosion to look like this 74.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 Dolar, euro etc...
The explode() was created with the intention of separating a string into an array of several smaller strings. For this he uses certain characters, passed by parameters, to make the separation.
In the programming, the gambiarra is a palliative (and creative) way to solve a problem or fix a system inefficiently, inelegant or incomprehensible, but still works.
This is a code that prints on the screen the repetition of the phrase "Hello world!" 5 times using for this 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 get 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;
}
Well, instead of 0 puts 1.
– Francisco
I tried to do this but returns empty field...
– Webster Moitinho
If any answer is useful, please mark it as accepted, see here why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252