I’m having an issue finding a solution here.. I’m developing a WordPress theme for a client that uses a for()
loop to iterate through the title of the page so it can be wrapped in <span>
s and displayed vertically.. the loop uses strlen()
to find the length of the title but since some of the page titles include ‘…’ or commas in the title it returns the html chars instead.. I can’t figure out what is causing that and every effort via htmlspecialchars_decode()
or html_entity_decode()
doesn’t work.. any suggestions? Is there something going on with the for loop that I’m now aware of?
Since it was requested here is the actual code:
$p_title = get_the_title($port_page->ID);
$title = '';
for($i=0;$i<strlen($p_title);$i++){
if(($p_title[$i])){
$title .="<span>$p_title[$i]</span>";
}
I’ve tried using mb_strlen as well.. the problem with searching for a specific character to replace doesn’t necessarily solve the problem since page titles are arbitrarily set by the site owner..
The weird thing is the Title is not encoded in any way and echo’s normally before the for loop.. So it’s as if something is converting it..
This sounds a lot like a character encoding issue with multibyte characters. Can you try replacing
strlen()
withmb_strlen()
and see if it does the job?http://php.net/manual/en/function.mb-strlen.php
strlen()
only returns the number of bytes in a string. Some special characters can be represented with multiple bytes, and Unicode can also make single ‘characters’ like a copyright symbol (“©”) occupy many characters (e.g.©
).Your “…” (ellipsis) can be a special character in Unicode for example.
The quick and dirty solution I suggest:
Note that I’m assuming your string is already UTF-8. If it isn’t, convert it first.