Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function substr() error

Tags:

php

substr

When I use substr() I get a strange character at the end

$articleText = substr($articleText,0,500);

I have an output of 500 chars and � <--

How can I fix this? Is it an encoding problem? My language is Greek.

like image 390
Stoikidis Avatar asked Sep 09 '25 15:09

Stoikidis


1 Answers

substr is counting using bytes, and not characters.

greek probably means you are using some multi-byte encoding, like UTF-8 -- and counting per bytes is not quite good for those.

Maybe using mb_substr could help, here : the mb_* functions have been created specifically for multi-byte encodings.

like image 172
Pascal MARTIN Avatar answered Sep 12 '25 03:09

Pascal MARTIN