Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Unicode Substring

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35656
    angelazou
    Participant

    Hi,

    I’m using the PHP’s substring function to obtain the first 15 Chinese characters in the post title. I’m using the following code:


    $post_title_str = get_the_title();
    if(strlen($post_title_str) > 45) {
    $post_title_str = substr($post_title_str, 0, 45);
    $output .= '

    At first, I used 15, but I noticed that everything after 5 characters are trimmed, so I multiplied by 3 and used 45 instead. The issue I’m having is that if the ending character is partially included (as mentioned above, 1 Chinese character seems to equate to 3 units of string length to PHP), a garbage character forms, like this 舟山市机关事务管理局2008年工作�…, notice the � at the end. So, I’m wondering if there is a special function that allows me to get the substring of a Unicode string, I’d appreciate any input, thanks.

    Angela

    PS: I’m getting a substring of the title itself, so if anyone happens to know a function similar to excerpt that I can apply to the header. That’d be great, thanks in advance.

    #92796
    angelazou
    Participant

    Well, I found a solution, I need to use mb_substr() instead. But I couldn’t figure out how to delete this post, so I just marked it solved.

    Angela

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.