简体   繁体   中英

Substr not working with html tags and entities

I have gone throught the following question: substr() not working but it did not work for me :(

I am facing the same problem. I am using nicEditor and for at the time of insert, I do htmlentities(addslashes(urlencode($description))) and when I view the description? It shows me correctly, but when i use substr() it returns nothing. like:

substr($description,0,10)

$description contains the content and it is fine, present in db, works without substr()

Please provide a var_dumb() of $description and a bit more code before $description is filled in, so we can see if there is an other problem.

Try this one

Use mb_substr for multibyte character encodings like UTF-8. substr just counts bytes while mb_substr counts characters.

substr() works with singlebyte only http://php.net/manual/en/function.mb-substr.php

Source: PHP Substr Function Trimming Problem

This happens because in UTF-8 characters are not restricted to one byte, they have variable length to match Unicode characters, between 1 and 4 bytes.

A safe way of cutting these strings without losing anything is by using the mb_substr PHP function instead. It works almost the same way as substr but the difference is that you can add a new parameter to specify the encoding type, whether is UTF-8 or a different encoding.

Source: http://osc.co.cr/extracting-a-substring-from-a-utf-8-string-in-php/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM