I try to use ascii letters in my url-site but I don't know what i use ascii letters ( è , é , à, ect.. ) in my url. I try to use urlencode\urldecode but i can't view my page when i use a ascii letters. I don't know what resolve this problem.
Can you help me with an example pls ?
function my_url($offer) {
$dd = url_title($offer->number." ".$offer->number,"-",true);
return site_url("siteweb\view".$offer->number."/".urlencode($dd));
My problem is in "urlencode($dd)"
update
function my_url($offer) {
$dd = url_title($offer->number." ".$offer->number,"-",true);
$ddd= rawurldecode($ddd)
return site_url("siteweb\view".$offer->number."/".htmlspecialchars($ddd));
I think what you're looking for is htmlentities
"htmlentities — Convert all applicable characters to HTML entities"
Related
I have many characters in a text / json like:
\xea = ê
\xaa = ª (I think)
How convert to correct character in PHP?
Thanks a lot.
You could try something like this:
$text = preg_replace_callback('/\\\\x([0-9a-fA-F]{2})/', function ($matches) { return chr(hexdec($matches[1])); },$text);
Edit: Sorry, I just copy/pasted an old code - the e modifier is deprecated.
I got a keyword var from an url like this:
search.php?keyword=%E5%AE%89%E5%85%A8
with utf8 encoding. Then I want to convert the keyword to this format:
安全
So I can use it in MySQL/PHP/
How can I achieve this?
This should work.
$html = mb_convert_encoding($_GET['keyword'], 'HTML-ENTITIES', 'UTF-8');
echo htmlspecialchars($html);
//安全
First one is url encoded, and what you want are html entities.
$string = urldecode('%E5%AE%89%E5%85%A8'); // == 安全
$string2 = htmlentities($string); // == 安全
$string3 = htmlspecialchars($string2); // == 安全
The one from your question is double encoded (& got converted to &) which I assume is wrong.
23433 is decimal and equals hexadecimal x5B89. Same for the second code. For the browser, it doesn't matter if it's decimal or hexadecimal.
If you really intend double encoding, use htmlspecialchars($string); on the above code.
i have small site where i display search results of posts titles,
some titles are up to 255 characters long and while displaying those in html table , the table's row breaks i.e. doesnt shows correct so i use substr php function to trim the title so that it can fit in table row.
For english titles it working great but for non-english titles it shows blank space i.e. trim everything.
i am using substr like this
<? echo htmlspecialchars(substr($row['title'],0,70)); ?>
so how can i make the non-english titles also of characters 70 ?
You should use multi-byte safe substr() operation based on number of characters for UTF-8:
mb_substr();
http://php.net/manual/en/function.mb-substr.php
http://php.net/manual/de/function.wordwrap.php
That might occur because substr is not multi-byte save function.
You can wether use mb_substr() instead - "http://de1.php.net/manual/de/function.mb-substr.php"
Or try function "wordwrap" because its simply made for cutting strings:
<? echo htmlspecialchars(wordwrap($row['title'], "70", "", true)); ?>
Another possibility is it that this happens when using only htmlspecialchars() without substr()? But this is just a suggestion incase my other two ideas do not help.
Try using this custom function i got from DataLife Engine CMS
function dle_substr($str, $start, $length, $charset = "utf-8" ) {
if ( strtolower($charset) == "utf-8") return iconv_substr($str, $start, $length, "utf-8");
else return substr($str, $start, $length);
}
like this
<? echo htmlspecialchars(dle_substr($row['title'],0,70)); ?>
I have a problem converting a string from cp1251 to utf8...
I need to get some names from database and those names are in cp1251(i'm not the one who made that database, so I can't edit it, but I know for sure that these names are cp1251)...
The name in database is this - "Р?нтернет РІ цифрах"
I'm converting it to utf8 using iconv function like this:
iconv("UTF-8", "CP1251//IGNORE", $name)
and what I have in the result is this - "�?нтернет в цифрах"(it's Russian), but the first two symbols are not correct... it should be "Интернет в цифрах"...
So the final thing that I have to do is somehow change these two symbols "�?" to russian letter "И"... and I really don't know how to do that... I've tried to use preg_replace, but it doesn't work...or I'm not using it correctly.
And I'm sorry for Russian letters, it is really hard to explain what I need without showing them.
The first letter comes out incorrect because one of the bytes needed to store the UTF-8 encoding of И (0x98 to be exact) is not used in CP1251. If the database has replaced the 98 byte by a question mark you have to change it back before using iconv:
$name = str_replace("\xD0\x3F", "\xD0\x98", $name);
echo iconv("UTF-8", "CP1251//IGNORE", $name);
use this:
mb_convert_encoding($model->text, 'cp1252', 'utf8')
Try this:
function cp1251_to_utf8($s){
$c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
for($i=0; $i<strlen($s); $i++) {
$c=ord($s[$i]);
if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
elseif ($c>239) $t.=$c209.chr($c-112);
elseif ($c==184) $t.=$c209.$c209;
elseif ($c==168) $t.=$c208.$c129;
else $t.=$s[$i];
}
return $t;
}
I would like to parse user inputs with PHP. I need a function which tells me if there are invalid characters in the text or not. My draft looks as follows:
<?php
function contains_invalid_characters($text) {
for ($i = 0; $i < 3; $i++) {
$text = html_entity_decode($text); // decode html entities
} // loop is used for repeatedly html encoded entities
$found = preg_match(...);
return $found;
}
?>
The function should return TRUE if the input text contains invalid characters and FALSE if not. Valid characters should be:
a-z, A-Z, 0-9, äöüß, blank space, "!§$%&/()=[]\?.:,;-_
Can you tell me how to code this? Is preg_match() suitable for this purpose? It's also important that I can easily expand the function later so that it includes other characters.
I hope you can help me. Thanks in advance!
You could use a regular expression to do that:
function contains_invalid_characters($text) {
return (bool) preg_match('/[a-zA-Z0-9äöüß "!§$%&\/()=[\]\?.:,;\-_]/u', $text);
}
But note that you need to encode that code with the same encoding as the text you want to test. I recommend you to use UTF-8 for that.