How to use str_replace to replace multiple words?

Asked

Viewed 119 times

1

I’m displaying a date of a post I’m pulling from instagram, but it displays like this:

Thu, 26 Sep 2019 13:47:05 GMT

It ta resulting in English names of each day of the week, but I want to remove it and take the name of the month 'Sep' and leave only the number of the month along with the date and remove the word GMT, would be equal:

26/09/2019 h:13:47:05

What I tried was:

<?php
$instaResult = simplexml_load_file('https://rss.app/feeds/wUySUmu0Kiy1c3uL.xml', NULL, LIBXML_NOCDATA) or die("error");
$instagram_photos = $instaResult->channel->item[0]->enclosure['url'];
//data da publicacao
$instagram_date = $instaResult->channel->item[0]->pubDate;

//Trocando nome do mes de ingles para portugues
echo str_replace("Thu", "Quinta", $instagram_date);


// descricao do instagram
$instagram_description = htmlentities((string)$instaResult[0]->channel->item[0]->description);
echo "</br>";
$result = trim(strip_tags(str_replace(array("<![CDATA[","]]>"),array("",""),(string)$instaResult[0]->channel->item[0]->description)));
 echo $result;

echo "<meta http-equiv='refresh' content='1800'; url='index.php'>"; //atualiza a cada 30 minutos
echo "<link rel='stylesheet' type='text/css' href='style.css?version=700'>";
echo "<div class='fundo'>";
echo "</div>";

// DEFINE O FUSO HORARIO COMO O HORARIO DE BRASILIA
    date_default_timezone_set('America/Sao_Paulo');
// CRIA UMA VARIAVEL E ARMAZENA A HORA ATUAL DO FUSO-HORÀRIO DEFINIDO (BRASÍLIA)
$hour = (int)date('G');
if ($hour >= 0 && $hour < 10)
{
	// 00:00 até 09:59
	$resp = "<img class='logo' src='logo.jpg' alt=''/>";
}
else
{
	// 10:00 até 23:59
	$resp = "<img class='img-responsive' src='{$instagram_photos}'/>";
}

echo $resp;
?>

If you can help me, I’d appreciate it!

1 answer

1


Use the constructor of the class 'Datetime' to obtain a 'Datetime' object from a format-compliant string UTC and method 'Datetime::format' to format the date to your liking.

Table for characters for format string for Datetime function::format' https://www.php.net/manual/en/function.date.php

<?php

   $instaResult = simplexml_load_file('https://rss.app/feeds/wUySUmu0Kiy1c3uL.xml', NULL, LIBXML_NOCDATA) or die("error");

   $instagram_date = new DateTime($instaResult->channel->item[0]->pubDate);

   echo $instagram_date->format('d/m/Y \h:H:i:s');

// Ou numa abordagem mais objetiva que produz o mesmo resultado
// echo (new DateTime($instaResult->channel->item[0]->pubDate))->format('d/m/Y \h:H:i:s');

?>

Code in the Repl.it

  • 1

    Thank you very much! It helped me a lot!

Browser other questions tagged

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