PhP get first letter as symbol � - php

This problem is the strangest thing I've seen for a while.
I got all pages in UTF-8, adding the <meta> as charset="UTF-8" in the index page, and even header().
Then I got a page where I add all the link from the top menu in an array, so it's scalable when showing the list.
$menu['services'] = "Services";
$submenu['services']['sendings'] = "International sendings";
/* And more like this */
To display the links:
foreach($service as $key => $value) {
if(isset($submenu[$key])) {
echo '<li>'.$value.'
<ul>';
foreach($submenu as $keysub => $valuesub) {
echo '<li>'.$valuesub.'</li>';
}
echo '</ul></li>';
}
else {
echo '<li>'.$value.'</li>';
}
}
This displays me all the menu correctly, except for the very first of all the submenu (just the first one).
<li>
Servicios
<ul>
<li>
Anternational sending
</li>
<li>
Parking service
</li>
<!-- and others -->
</ul>
</a>
</li>
As you can see, there is an "A" instead of and "I". I tried other words, like "Envíos internacionales" (in spanish), and outputs "�nvíos internacionales".
I really don't know why it's doing this.

Make sure all your PHP files are saved using UTF-8 without BOM (byte order mark), you can easly check what encoding is currently set and convert files using freeware notepad++. There is "Format" option in main menu, you can either set or convert currently opened file to desired encoding. You can also check for byte order mark using some generic hex editor.

Even though your page is in UTF8, make sure that your DB table is in UTF8 encoding (If the mentioned data is been populated from DB). You could also try using utf8_encode or iconv .
This may also happen if there exists one or more encoding types are declared in a page

Related

wordpress double quotes are converting to inverted commas thats why design is breaking

I am wring a wordpress plugin where I have to show some data in bootstrap popover
for that I am populating some information in data attributes
it is working on my local server, also it is working on live server if I am rendering my content inside an iframe.
but when I am rendering my HTML inside wordpress content then it is breaking down.
below is my code
<?php $iTag = " <i class='float-end btn-popover btn-close'></i>"; ?>
<i class="fa fa-info-circle ms-3 popover-handler no-collapsable"
data-bs-title="<?php echo str_replace('"',"'",$item['title']) . $iTag; ?>"
data-html="true" data-bs-content="<?php echo str_replace('"',"'", stripslashes($item['meta_info'])); ?>"></i>
And below is the output
if you notice data-bs-title value is started with double quotes but ended with inverted commas, actually it is considering data-bs-title value untill next double qoutes which I mard with green.
while debugging I did many things like
I tried removig class attribute from i tag but still it was not fixed
I also tried removing whole i tag than it worked, so it means there is something with <> signs?
WordPress has wptexturize(), which according to their documentation "replaces common plain text characters with formatted entities." (https://developer.wordpress.org/reference/functions/wptexturize/).
This is what is happening in your case. Please look at the "more information" section in the above linked documentation page on how to "get rid" of it.
The most at hand solution would be
add_filter('run_wptexturize', false);
but you might not want to fully deactivate it.
You might want to use shortcodes (their content is not wptexturized) or <script> tags instead.

Danish letters MySQL output

I got some varchar fields in my MySQL database containing danish letters (æ, ø, å). When browsing the database with phpmyadmin, the letters appear correctly, however when I obtain the field through a query, and try to display the field, they are changed ("ø" becomes "ø"). I tried changing the collation to both latin1 and utf-8 (both danish versions), but without luck. I can't even figure out if it is the database or my code that is the issue. Anyone who has seen this before?
Edit: I'm adding the code to read and display the database content. The issue is confirmed in the "users_last_name" and "address_street", but is likely present all over (currently these are the only fields with danish letters).
Code:
<?php
// Query to load information on projects
$main_query = $this->db->query('SELECT project_id, project_name, project_image_src, project_owner FROM ed_projects');
foreach ($main_query->result() as $row) {
// Get adress of the current project in the "foreach" loop
$project_id = $row->project_id;
$address_query = $this->db->query("SELECT * FROM ed_project_address WHERE project_id='$project_id'");
$address_row = $address_query->row();
// Get the name of the user who owns the current project
$user_id = $row->project_owner;
$user_query = $this->db->query("SELECT users_first_name, users_last_name FROM ed_users WHERE id='$user_id'");
$user_row = $user_query->row();
?>
<div class="projectAvatar">
<?php if ($row->project_image_src) {
echo "<img src=".$row->project_image_src.">";
} else {
echo "NoImg";
}
?>
</div>
<div class="projectInformation">
<?php echo $row->project_name; ?> <br />
<?php echo $address_row->address_street." ".$address_row->address_number; ?> <br />
<?php echo $user_row->users_first_name." ".$user_row->users_last_name; ?> <br />
</div>
<?php
}
If things are working in phpMyAdmin, but not on your own web pages, it's likely to be a problem with the character encoding of your web pages. Assuming you're using an HTML5 doctype, try just adding:
<meta charset="utf-8">
to the <HEAD> section of your site.
Basically, from your comments and code, it seems like you're successfully storing your Danish characters using UTF-8 encoding in your database. (The table's collation setting won't affect that; collations determine sort orders and comparisons, but not the actual character set used for storage.) To make characters appear correctly on a web page, you need to tell the browser what character encoding you're using for your page; adding the <meta charset...> header does this. phpMyAdmin's web pages will almost certainly be using UTF-8 as their character set, so if that's working, you should change your pages to match it.
As I mentioned, I feel that by far the best full explanation of how this all works is given in Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!), which will tell you about how this stuff should be done in all its gory detail.
try this ANSI (Windows-1252).
read from
Differences Between Character Sets

Wordpress foreign language in .php

I'm editing a site for someone, and they are using wordpress, which I really don't like, but hey, I didn't pick it. I need to change some text on their page to Portuguese characters such as Ç or Ã. I've read in a few places, that I need to change from ASCII to UTF-8, but I'm not sure where to do that, or how to do it across the whole site. Am I changing a database to UTF-8, or each individual php file? Hopefully somebody knows, thanks.
Thanks to the comments below, I have most of the site running correctly, but now I can't get the foreign characters in just certain spots, for example, anywhere I'm using code like this inside of a .php file.
$email_list = do_shortcode('[pl_modal title="Join our email list" label="<img class=\'\' title=\'Join our email list\' src=\'/wp-content/uploads/2013/02/email_icon.png\' /><br /><span>INSCREVA-SE A NOSSA<br />LISTA DE E-MAILS</span>"][gravityform id=1 title=false][/pl_modal]');
The portugese in the above code, if I add non english characters, I get a constantly loading error. More code, that does the same thing.
'<div class="graphicbuttons_cont">' .
'<a href="https://maps.google.com/maps?saddr={19}&daddr={20}" target="_blank">
<img title="Get Store Directions" src="/wp-content/uploads/2013/02/getdirection_icon.png" /><br /><span>LOCALIZACOES <br><br /> </span>
</a>' .
'</div>' .
the LOCALIZACOES in above text, should have special characters, but it won't hold them. I have changed everything to UTF8 that I can find. But there is nothing inside this specific file that says utf8, should I add something?
Alright, so, if you change everything to utf8, and on wordpress all of your html code is in php files, the way I've used to use special characters is this
thesauruslex.com/typo/eng/enghtml.htm
for example
<span>LOCALIZA&CcedilOES </span>
will output LOCALIZAÇOES
Thanks to everyone for the help, I guess I could have been clearer on the original question.
Everything in your application needs to be UTF-8.
Your MySQL string columns should be utf8_unicode_ci.
You need to ensure that your MySQL connection charset is set to UTF-8. You can do this via the query SET NAMES utf8 (run once after every connection) or you can modify your my.cnf file if you have access to it.
Your web pages should be served with <meta charset="utf-8">
You can check and validate what kind of input you're receiving by using the PHP function mb_check_encoding.
There's also a PHP ini setting called default-charset.
This can be changed two ways depending on your theme file. In the header.php file this should be near the top:
<meta charset="<?php bloginfo('charset'); ?>">
You use to be able to change this in the wordpress backend under settings -> reading. I believe now you have to manually change this in the wp-config.php file:
define('DB_CHARSET', 'utf8');

Convert numbered lists text to nested HTML lists

UPDATE AT THE BOTTOM
Maybe somebody could help with this... been struggling with it for days and i'm blocked :/
For a content-cleaner solution i'm working in, i'm trying to convert some pure-text numbered lists, like:
1 Foo
1.1 Foo 1
1.2 Foo 2
2 Bar
2.1 Bar 1
2.2 Bar 2
2.2.1 Bar 2.1
2.2.2 Bar 2.2
2.3 Bar 3
3 Z Another root item
... into correct nested html lists ...
<ul>
<li>Foo
<ul>
<li>Foo 1</li>
<li>Foo 2</li>
</ul>
</li>
<li>Bar
<ul>
<li>Bar 1</li>
<li>Bar 2
<ul>
<li>Bar 2.1</li>
<li>Bar 2.2</li>
</ul>
</li>
<li>Bar 3</li>
</ul>
<li>Another root item</li>
</ul>
Some things that may help:
No need for the result to be correctly indented, just surrounded by the correct html tags
No need to locate the list inside another text, can sume i already have only the list
No need for great performance, regexp, itaration... whatever works is fine
No need for especific language solution, PHP, Python, Javascript, Pseudocode... is fine
Can asume " " (space) as the only separator after the "1.2.3 " list text
Can asume lines are already in the correct order, no need to order them at all
UPDATE TLTR (Not homework, but real world usage)
Sorry for looking so "homework not done", my fault. English is not my language and i tried to be maybe to concise.
What i'm trying to do is to make it easier for my workmates to format text to correct html from unknow sources.
Up to day i managed to (you can see the full screenshot here http://twitpic.com/907aw5/ as i can't attach images being my first question and no reputation):
I get the original text and do a strip_tags on it to delete any incorrect HTML it can have
I insert it into a textarea
I integrated a Javascript editor ( Codemirror http://codemirror.net ) with the specifications for HTML
I injected an edition bar with the most common tags we use, as my workmates doesn't know a word about HTML
As part of the cleaning options, i set two hotkeys that makes an ul / ol of the selected text (breaking in the \n chars)
When the user saves, i run HTMLTidy on it for it to became as cleaner as posible (indent, delete propietary tags, etc...)
Just to finish, as you can see in the above screenshot, i have a lot of texts with the 1.2.3 "organization", and it will be of much help to be able to get a nested list solution out of this kind of text.
UPDATE (The especific needs)
Now the explanation of "why" i used so many bullets for asumptions:
No need for the result to be correctly indented, just surrounded by the correct html tags (Because after this, when the user hit Save button, i run htmltidy on it, so it get indented)
No need to locate the list inside another text, can sume i already have only the list (Because i run the code over the user-selected text in the editor, so i can sume he selected the correct list)
No need for great performance, regexp, itaration... whatever works is fine (As it an human-use, point-click, point-click, i don't mind if it takes 0.0001 seconds per use, or 0.1)
No need for especific language solution, PHP, Python, Javascript, Pseudocode... is fine (I intend to use it in javascript/jQuery, but what i need is just the logic, as i'm blocked... i can tarnslate it if the solution is in another language)
Can asume " " (space) as the only separator after the "1.2.3 " list text (As it is the 99% of my text-cases)
Can asume lines are already in the correct order, no need to order them at all (As you can see in the screenshot, that text is human-entered, and i asume they inserted it in the correct order)
Sorry again for not being clear enought, just my first question in Stackoverflow, and i didn't realize it will look like homework, my fault.
Just for funsies, I went ahead and wrote a solution to your problem using PHP:
function helper_func($m)
{
static $r=0;
$o='';
$l=preg_match_all("#\d+#",$m[1],$n);
while($l < $r)
{
$r--;
$o .= '</li></ul>';
}
if($l == $r)return $l == 0?$o.$m[0]:$o.'</li><li>'.$m[0];
else $o=$m[0];
while($l > $r)
{
$r++;
$o = '<ul><li>'.$o;
}
return $o;
}
echo preg_replace_callback("#^([0-9.]*).*$#m","helper_func",$input);
However, in deference to this being homework, I included a deliberate error: for it to come out correctly, you need to make a single small change to $input before passing it in... Have fun :)

How to number things in PHP?

UPDATE:
I know I can use <ol> directky in the output but I remember using something like:
<?php echo $i++; ?> when I worked on a wordpress blog once. Every time I inserted that tag a number greater than the previous appeared so I basically did:
<?php echo $i++; ?> Text
<?php echo $i++; ?> Text
<?php echo $i++; ?> Text
I'm a front end guy (HTML/CSS) so please excuse this basic question. I just need to know what code in PHP I can use to number some text.
Text
Text
Text
into:
Text
Text
Text
Kind of like what <ol> does in html but in PHP.
Updated answer:
You can use a variable as you already do (the example you are posting should already work). Just initialize it using $i = 0;
Old answer:
You have a fundamental misunderstanding here. PHP is a scripting language, not a markup language. PHP does operations like connecting to data sources, calculating, making additions, changing entries in databases, and so on. PHP code, in short, is a series of commands that are executed. PHP has no design elements, tags and formatting options in itself.
PHP can (and usually does) output HTML (Where you have <ol>) to display things.
You can have an array of arbitrary data in PHP, coming from a file or data source:
$array = array("First chapter", "Second chapter", "Third chapter");
you can output this data as HTML:
echo "<ol>";
foreach ($array as $element) // Go through each array element and output an <li>
echo "<li>$element</li>";
echo "</ol>";
the result being (roughly)
<ol>
<li>First chapter</li>
<li>Second chapter</li>
<li>Third chapter</li>
</ol>
It depends on what type of file you are trying to write. Most often, PHP is writing a webpage in HTML, but not always. In HTML, if you want a numbered list, you should use an ordered list (<ol>).
If you're just writing a text file of some kind, incrementing and outputting a variable (like $i in your example) should work.
You mention Wordpress, so it's worth noting that if you worked on a Wordpress template before, you were using dozens of special functions in the Wordpress library, even though you may not have been completely aware that was what you were doing. A lot of the PHP heavy lifting is hidden and simplified for the templating engine, and if your current project is not built on that engine, you will have to do that logic yourself.

Categories