Yii2: StringHelper::truncate returns a string with more space

Created on 8 Sep 2016  Â·  11Comments  Â·  Source: yiisoft/yii2

echo \yii\helpers\StringHelper::truncate('我<span>是a中</span>国人', 4, '...', 'UTF-8', true);

When I set param of $asHtml are true, the result is: "我 <span>是a </span>..."

But I'm expect is: "我<span>是a中</span>..."

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.9 |
| PHP version | 7.0.1 |
| Operating system | windows 10 |

bug

Most helpful comment

All 11 comments

Seems like unicode issues to me.

  1. Have you tried code from master?
  2. Any ideas on fixing it?

I'm sorry I don't have try to code from master, bacause I no any ideas

OK, I'll handle it.

That's not because of UTF-8 but because of adding extra spaces (why?) after tokens here

@samdark Thanks

Isnt this the cause of half widht characters? In the pull ruest for console::table the widh was also off.

Isnt this the cause of half widht characters?

No, it's not

I can't remember why I added the extra space there, not sure if it was more for truncating words within HTML that spaces would get lost.
I'll do some more tests to see if that space can be removed.

The main issue is with spaces getting removed by the truncate and truncateWords methods.
I've updated the truncateHtml method and added new tests which all pass.

Resolved by 6b88f2f

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpodpro picture jpodpro  Â·  3Comments

nokimaro picture nokimaro  Â·  3Comments

indicalabs picture indicalabs  Â·  3Comments

Locustv2 picture Locustv2  Â·  3Comments

psfpro picture psfpro  Â·  3Comments