473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

substr on UTF-8 strings returned from MySQL

I am trying to truncate some Chinese Text returned from MySQL. If I use the
substr function, then the last one or two chinese words would appear as
symbols as opposed to the word it should be displaying? The PHP page itself
is already UTF-8 encoded

What is the best way of truncating such UTF-8 strings (e.g., to return the
first 50 chinese "words"). Chinese words appears like
"?????????????????????" (21 chinese "words" shown here).
Jul 17 '05 #1
2 3161
peter <pe***@mail.co.uk> wrote:
I am trying to truncate some Chinese Text returned from MySQL. If I use the
substr function, then the last one or two chinese words would appear as
symbols as opposed to the word it should be displaying? The PHP page itself
is already UTF-8 encoded
Behold: http://nl2.php.net/manual/en/ref.mbstring.php

The multibyte string functions
What is the best way of truncating such UTF-8 strings (e.g., to return the
first 50 chinese "words"). Chinese words appears like
"?????????????????????" (21 chinese "words" shown here).


mb_substr will do the trick....

--

Daniel Tryba

Jul 17 '05 #2

"Daniel Tryba" <ne****************@canopus.nl> wrote in message
news:ce**********@news.tue.nl...
peter <pe***@mail.co.uk> wrote:
I am trying to truncate some Chinese Text returned from MySQL. If I use the substr function, then the last one or two chinese words would appear as
symbols as opposed to the word it should be displaying? The PHP page itself is already UTF-8 encoded


Behold: http://nl2.php.net/manual/en/ref.mbstring.php

The multibyte string functions
What is the best way of truncating such UTF-8 strings (e.g., to return the first 50 chinese "words"). Chinese words appears like
"?????????????????????" (21 chinese "words" shown here).


mb_substr will do the trick....

--

Daniel Tryba


The PCRE engine of newer version of PHP is also capable of dealing with
UTF-8 text. preg_match('/(.{1,50})/u', $s, $m) would yield a string of 50
characters.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
I've a template with some PHP code in it. I need to get the names of all the PHP commands, so I can import them and so I can make sure they are officially allowed (for security purposes, users are...
5
by: MuffinMan | last post by:
Hi, I'm trying to add up an integer with the result from substr() like $second_int = substr( .... ) + $int; I know the result from substr() is an integer and so is $int but in the end...
1
by: Kim Gijung | last post by:
hi all i looped bellow code around 100 times and the results are following. each reaults are millisecond. is the substr takes much performance? and don't know why the time it takes getting...
1
by: Patrick Londema | last post by:
Hi, I'm trying to write a function that takes a string and splits this into 3 distinct sections for a project I'm working on. This string in question has the following formet: "% ...
2
by: Ari | last post by:
we have a strange problem with oracle dynamic sql method 4. when selecting a substr we expect to, and normally, receive from oracle in the describe select list (SELDSC) the length of the...
7
by: entropy123 | last post by:
My problem is with the following test code: #include<string>; std::string TestBuff; TestBuff.append("A + B <==> C + D"); std::cout << "TestBuff.length():" << TestBuff.length() << endl;...
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
6
by: sks | last post by:
Hi, Here is a small program that is wrtten to simply use substr. When the second parameter in substr (length of the string to be extracted) is lesser than 0, the output is the entire string (I'm...
3
by: kocek | last post by:
Hi! If I create an index using function Substr (e.g.: create index on SUBSTR(afield, -2) ) then if I use the substr on the given field, with the given parameter in a select, but I do not type...
3
by: jmckown | last post by:
I'm attempting to pull a list of Google search terms from url's stored in a database. Here is an example of the data in the table I'm dealing with http://www.google.com/search?hl=en&q=proxy...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.