Simple RSS encoding issue - php

Consider the following PHP code for getting RSS news on a site I'm developing:
<?php
$url = "http://dariknews.bg/rss.php";
$xml = simplexml_load_file($url);
$feed_title = $xml->channel->title;
$feed_description = $xml->channel->description;
$feed_link = $xml->channel->link;
$item = $xml->channel->item;
function getTheData($item){
for ($i = 0; $i < 4; $i++) {
$article_title = $item[$i]->title;
$article_description = $item[$i]->description;
$article_link = $item[$i]->link;
echo "<p><h3>". $article_title. "</h3></p><small>".$article_description."</small><p>";
}
}
?>
The data accumulated by this function should be presented in the following HTML format:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Новини от Дарик</title>
</head>
<body>
<?php getTheData($item);?>
</body>
</html>
As you see I added windows-1251(cyrillic) and utf-8 encoding but the RSS feed is unreadable if I don't change the browser encoding to utf-8. The default encoding in my case is cyrilic but I get unreadable feed. Any help making this RSS readable in cyrilic(it's from Bulgaria) will be greatly appreciated.

I've just tested your code and the Bulgarian characters displayed fine when I removed the charset=windows-1251 meta tag and just left the UTF-8 one. Want to try that and see if it works?
Also, you might want to change your <html> tag to reflect the fact that your page is in Bulgarian like this: <html xmlns="http://www.w3.org/1999/xhtml" lang="bg" xml:lang="bg">
Or maybe you need to force the web server to send the content as UTF-8 by sending a Content-Type header:
<?php
header("Content-Type: text/html; charset=UTF-8");
?>
Just be sure to include this before ANY other content (even whitespace) is sent to the browser. If you don't you'll get the PHP "headers already sent" error.

Maybe you should take a look at htmlentities.
This can convert to html some characters.
$titleEncoded = htmlentities($article_title,ENT_XHTML,cp1251);

Related

PHP - Echo is not displaying correctly

https://pastebin.com/pEg8ZARP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Future Value</title>
</head>
<body>
<?php # Script 1.8 - futureValue.php.
// Set the variables:
$interest = .08;
$amount = 1000;
$years = 20;
// Calculate the value:
$value = $amount* pow( (1+$interest), $years);
// Format the total:
$value = number_format ($value, 2);
// Print the results:
echo "<p>The future value is <b>/$$value</b>.</p>";
?>
</body>
</html>
I tried pasting it here but the formatting got messed up.
Whenever I try to load the page it just says " The future value is /$$value.
"; ?> " instead of displaying "The future value is $4660.96.".
I am totally lost at what I am doing wrong.
echo "<p>The future value is <b>$".$value."</b>.</p>";
$ is a special character so you need to escape it. Or you can always just concat your string and variable to avoid this problem.

How to output Chinese in HTML file

I have a form and insert some chinese words in database and it's ok. Table charset is UTF8. Problem appears when I select this data and send it via mail as HTML attachment.
Then, Chinese doesn't display properly. How to fix charset before send data via mail? Should I use some headers and will it work?
My code looks like that:
//$attachedBodyContent is data from database that contains some chinese words
Mail::send(
"emails.applicationTemplate",
$data,
function($message) use ($data, $template, $subject, $attachedBodyContent) {
$message->to($data['email'], $data['name'])
->from($template['from'],$template['from_name'])
->subject($subject)
->attachData($attachedBodyContent,'YourApplicationData.html');
}
);
When you generate .html attach file you should include in your <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
In this case you can use this code for merge your content with <head>
<?php
$header = '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>';
$footer = '</body>
</html>';
$allContent = $header.$attachedBodyContent.$footer;
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
This should do it, for further information check the link.
http://www.inventpartners.com/chinese-chars

Generate div automatically by mysql_fetch_array

I would like to create a system to issue opinions on a given thing. I'm at a good point, but I can not get created for each element taken from the database a correspond div. Let me explain, for example if I have three reviews made by the three authors, I would like three divs for the reviews, and three for the authors, then automatically generated by the loop. How can i do?
You can do this.. First of all add <body> tag to your html document
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$Content = mysql_query("SELECT * FROM feedback");
while($Row = mysql_fetch_array($Content)) {
$Feedback = $Row['FeedbackUser'];
$UserNick = $Row['UserNickname'];
$UserMail = $Row['UserEmail'];
echo "<div>{$Feedback}</div>"; // Displaying feedback as div
echo "<div>{$UserNick }</div>"; // Displaying UserNick as div
echo "<div>{$UserMail }</div>"; // Displaying UserMail as div
}
?>
</body>
Hope this is what you are looking for

How to call XML data in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A simple program to CRUD node and node values of xml file
I tried calling the data in my XML in the below way. But it didn't work for me. Can anybody tell me how can I make it work?
<broad>
<site>
<title>My Site Name</title>
<caption>My Site Caption</caption>
<hostname>www.mydomain.com</hostname>
</site>
<broad>
My PHP file is
<?php
$xml = simplexml_load_file('settings.xml');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $xml->title; .'|'. $xml->caption; ?></title>
</head>
<body>
</body>
</html>
Why the above way is not working? is there any other way which is more easier than this?
You need to access the site node:
<?php echo (string) $xml->site->title?>
If ever in doubt, use a var_dump:
<?php echo '<pre>'; var_dump($xml); echo '</pre>'; ?>
Use the following:
<?php echo (string)$xml->site->title .'|'. (string)$xml->site->caption; ?>
Use SimpleXML debug with var_dump() and print_r()

Converting russian characters from upper case to lower case in php

I'm trying to change the case of russian characters from upper to lower.
function toLower($string) {
echo strtr($string,'ЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ','ёйцукенгшщзхъфывапролджэячсмитьбю');
};
This is the function I used and the output looks something like this
ЁЙ## ёѹ##`
Can anybody help me with this ?
Thanks in advance
$result = mb_strtolower($orig, 'UTF-8');
(assuming the data is in utf-8)
Specify the charset within the HTML and use mb_strtolower() to convert case:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<head>
<title></title>
</head>
<body>
<?
$string = 'ЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ' ;
echo mb_strtolower($string, 'UTF-8');
?>
</body>
</html>
With the meta-tag it looks like this:
цукенгшщзхъфывапролджэячсмитьбю
Without the meta-tag it looks like this
цукенгшщзхъфывапролджÑÑчÑмитьбю

Categories