Resumen

Se explicara la manera correcta de acortar strings que se encuentran codificados en formato utf-8.

 

Ambiente

-PHP

Explicación

Normalmente cuando se requiere extraer o acortar una cadena de datos se utiliza la función de php llamada substr

 

substr(string $string, int $start, int $length = ?)

 

Devuelve una parte del string definida por los parámetros start y length.

El problema es que esta función al utilizarlo con cadenas de datos codificadas en formato UTF-8 cuenta mal los carácteres, por lo que podría generar carácteres dañados y mostrandolos de esta manera: �.

 

Para evitar estos inconvenientes de deberá usar la función mb_substr.  Esta función cuenta correctamente los caracteres y retorna la cantidad correcta.

 

mb_substr(
    string $str,     int $start,     int $length = NULL,     string $encoding = mb_internal_encoding() )

Realiza una operación substr() multibyte de forma segura basada en el número de caracteres. La posición se cuenta desde el principio de str. La posición del primer carácter es 0. La posición del segundo es 1, etc.

 Ejemplo


        $utf8string = "Buenos días compañeros";

        echo substr($utf8string,0,17);
        // output Buenos días co
        echo mb_substr($utf8string,0,17,'UTF-8');
        //output Buenos días comp

Referencias Externas

  1. https://www.php.net/manual/es/function.substr.php
  2. https://www.php.net/manual/es/function.mb-substr.php
Categorías: PHP

22 comentarios

jjjbetcom · diciembre 3, 2025 a las 8:09 pm

JJJbetcom, huh? Sounds kinda catchy. Browsed through the site and they got a solid selection of sports and games. Could be worth a punt. Your thoughts? Check it out jjjbetcom.

kingphisher · diciembre 19, 2025 a las 7:03 pm

I saw kingphisher.info. Definitely something there if that’s the kind of thing you’er looking for. It sounds fun! Check it out for yourself: kingphisher

76xgame · diciembre 19, 2025 a las 10:13 pm

76xgame popped up on my radar. Decent games, not too shabby at all. If you’re bored, give it a try. Here’s the link: 76xgame

slotphlogin · enero 14, 2026 a las 9:32 am

slotphlogin https://www.exslotphlogin.net

6gbetvip · febrero 8, 2026 a las 6:08 pm

Thinking about going VIP with 6gbetvip. Anyone had any experience with their program? The perks are looking pretty tempting! Give it a shot: 6gbetvip

zalv com · febrero 8, 2026 a las 6:08 pm

Yo fam, zalv com is where it’s at! Seriously, been spendin’ way too much time there lately. Give it a go, you might just get hooked! Check it out here: zalv com

10brl · febrero 8, 2026 a las 6:08 pm

10brl, huh? Haven’t tried it myself, but it’s been makin’ some noise. Might be worth lookin’ into! Check it out here: 10brl

SBOTOP · febrero 9, 2026 a las 1:24 pm

[6538]SBOTOP Login & Register | Best Slot Games, App Download & Official Link Alternatif Philippines Join SBOTOP Philippines! Quick SBOTOP login & register for premium SBOTOP slot games. Get the SBOTOP app download & official SBOTOP link alternatif for secure play. visit: SBOTOP

Jacobjap · marzo 3, 2026 a las 3:21 pm

Если нет желания заниматься настройками, доступна аренда хрумера https://www.olx.ua/d/uk/obyavlenie/progon-hrumerom-dr-50-po-ahrefs-uvelichu-reyting-domena-IDXnHrG.html, позволяющая быстро начать работу.

goo88dapk · marzo 13, 2026 a las 9:33 pm

Fancied a go and got the goo88dapk apk. Easy to install and runs a treat. Decent graphics, not lagging at all. Check it out for yourself goo88dapk!

zs777loqin · marzo 13, 2026 a las 9:34 pm

zs777loqin, just fired it up. Had no problems getting logged in, good thing as I hate messing around. Not blown away. Still worth a punt. Go login zs777loqin!

ne88bet · marzo 13, 2026 a las 9:34 pm

Had a crack at ne88bet. Nothing special but it worked well on my phone. I’d advise checking it out for yourself ne88bet!

balislot88 · agosto 30, 2026 a las 2:43 pm

If you love slots you cannot miss this one. The hit rate seems much better here than on other platforms I have used. Give balislot88 a try.

bdmostplaybet · septiembre 5, 2026 a las 3:17 am

I’ve tried a few platforms lately but this one really stands out. The interface is super smooth and the payouts are fast. Definitely my new go-to spot! bdmostplaybet

123jiliapp · octubre 12, 2026 a las 4:11 pm

The app version is just fantastic and runs perfectly on my phone. So convenient to play anywhere. Get the 123jiliapp now!

lucky97login505 · octubre 12, 2026 a las 11:19 pm

I’ve been looking for a reliable place to play and this one really delivers. The interface is super smooth and the payouts are fast. Check it out at lucky97login505

liga2000slot · octubre 17, 2026 a las 3:04 am

I’ve been looking for a reliable slot site for a while and this one really delivers. The gameplay is smooth and the payouts feel fair. Definitely worth checking out liga2000slot

989betvip · octubre 20, 2026 a las 4:53 pm

The VIP treatment here is real. I feel valued as a player and the bonuses are actually worth it. Join 989betvip

phjljl2 · octubre 30, 2026 a las 6:37 pm

Best platform I’ve used in the Philippines so far. The games are exciting and it feels very fair. Highly recommend giving phjljl2 a try if you love winning.

09tr88 · noviembre 1, 2026 a las 8:56 am

I was skeptical at first but after a week of play I can say this is a legit platform. Withdrawals are processed without any hassle. Cheers to 09tr88!

shwecasinoapplink · noviembre 4, 2026 a las 8:43 am

Downloaded it yesterday and the performance is absolutely smooth on my phone. Graphics look crisp and I never experience lag during busy hours. It feels like carrying a premium club right in my pocket wherever I go. Highly satisfying for casual players and regulars alike. shwecasinoapplink

trumtopvn · noviembre 18, 2026 a las 9:51 pm

I am a regular player and this platform has quickly become my go to. The live dealer games are top notch and withdrawals never take longer than expected. Definitely worth checking out if you want a seamless experience. trumtopvn

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *