php substr() function with utf-8 leaves � marks at the end
The comments above are correct so long as you have mbstring enabled on your server.
$var = "Бензин Офиси А.С. также производит все типы жира и смазок и их побочных продуктов в его смесительных установках нефти машинного масла в Деринце, Измите, Алиага и Измире. У Компании есть 3 885 станций технического обслуживания, включая сжиженный газ (ЛПГ) станции под фирменным знаком Петрогаз, приблизительно 5 000 дилеров, двух смазочных смесительных установок, 12 терминалов, и 26 единиц поставки аэропорта.";
$foo = mb_substr($var,0,142, "utf-8");
Here's the php docs:
http://php.net/manual/en/book.mbstring.php
arabic echoing substr it gives me ? mark on end of my data been echoed
It’s definitely a UTF-8 encoding issue. And the solution is to use mb_substr
instead of plain substr
like so:
<? echo mb_substr($row['text'],0,500,"utf-8"); ?>
Using PHP's substr() with special characters at the end results in question marks
If your string has multibyte encoding (like UTF-8) does, you should use mb_substr
to avoid problems like this:
$introtext=mb_substr($introtext,0,200);
PHP substr() function is not working as expected
I managed to solve the problem by using utf8_decode
, i.e.:
$amount = utf8_decode('€300');
echo substr($amount ,1);
//300
Why slug function leaves a - at the end of title if there is any symbol?
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
In this line, you are replacing all non-alphanumeric characters with the separator, and ?
is not an alphanumeric character, so it is replaced, and you do nothing to remove it later.
You can add a $string = trim($string, $separator);
at the end of the function to remove trailing $separator
characters at the end of the string.
substr that won't cut out word with dynamic content
This is the way to do without using regex :
<?
$position=200;
$post = substr($row_News['teaser'],0,$position);
$post = substr($post, 0, strrpos($post, ' '));
echo $post."....";
?>
Reference : Making sure PHP substr finishes on a word not a character
substr function displaying a blank page
You're not getting a blank page. You're probably getting a rendered HTML page that happens to contain <TABLE
. Do a 'view source' to confirm.
How to remove question mark (character string) with PHP
I think the problem is not php not understanding the char but the browser.
Simply change it to HEXCHAR;
but don't forget to replace HEXCHAR
with the hexadecimal unicode representation of your character.
If you remove the x
, you'll have to use the decimal representation of the char.
For example, the character A
would be A
or A
In your case, it would be:
'ipod ا ج د د ا ص د ا ر م ن ا ب ل'
If you do this, don't forget to adjust the substr
and replace
method.
Escaping the chars could also be automated by getting the unicode representation of each char and replacing the char with , the hexadecimal representation and
;
.
Related Topics
How to Send Email from Localhost Using PHP on Linux
Cakephp Does Not Use My Models
How to Get Insert Id in Mssql in PHP
How to Easily Switch Between PHP Versions on MAC Osx
PHP Syntax Question: What Does the Question Mark and Colon Mean
Pdo + MySQL and Broken Utf-8 Encoding
Converting a Number Base 10 to Base 62 (A-Za-Z0-9)
How to Get Unique Value in Multidimensional Array
Ssl Error Ssl3_Get_Server_Certificate:Certificate Verify Failed
Return the Portion of a String Before the First Occurrence of a Character in PHP
How to Get Closest Date Compared to an Array of Dates in PHP
How to Enable MySQLi Support for PHP's Cli