# mbstring, a proposition of additional functions

- Thread: https://externals.io/message/62072
- Original archive: https://news-web.php.net/php.internals/62072
- Started by Ivan Enderlin @ Hoa on 2012-08-06T19:54:35+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62072 — Ivan Enderlin @ Hoa — 2012-08-06T19:54:35+00:00

URL: https://externals.io/message/62072#62072

Hello,

ext/mbstring is very useful, but from my point of view, some functions 
are missing.
I would like to propose the addition of the following functions.

mb_append($str, $piece) and mb_prepend($str, $piece):
To add a $piece to, respectively, the end and the start of $string. It 
should consider the text direction, e.g. if $str is Right-to-Left, 
mb_append() will add $piece to the end, so the left, of $string.

concat($str, $piece):
If mb_append() and mb_prepend() are too hard to understand by users, 
concat() would be pretty much simpler and will act in the same way.

mb_pad($length, $piece, $end):
Pretty much like strpad but with multi-bytes considering.
Note that, like mb_append and mb_prepend, we do not speak about left and 
right, but start and end.

mb_get_direction($str):
Return LTR or RTL. For one char, it is “easy”. For a string, it is more 
complex because we can have embedding directions (a string with many 
directions).

mb_get_char($code) or mb_chr($code):
Get Unicode character from a code-point/decimal value representation.

mb_get_code($char) or mb_ord($char):
Get code-point/decimal value representation from of Unicode character.

Finally, it would be great if `mb_substr()` could consider text direction 
(or create a new dedicated function?). For example, mb_substr($str, 0, 
1) will return the first char from the start (and not the left as it is 
implemented now).

I think it could help developers to create nice libraries without 
requiring a lot of skills in Unicode.
Thoughts?

Best regards :-).

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #63227 — Ivan Enderlin @ Hoa — 2012-09-24T14:41:30+00:00

URL: https://externals.io/message/62072#63227 · In reply to: #62072

Hi,

I sent this email during this summer and nobody replied. I would like to 
know your opinion.



> Hello,
>
> ext/mbstring is very useful, but from my point of view, some functions 
> are missing.
> I would like to propose the addition of the following functions.
>
> mb_append($str, $piece) and mb_prepend($str, $piece):
> To add a $piece to, respectively, the end and the start of $string. It 
> should consider the text direction, e.g. if $str is Right-to-Left, 
> mb_append() will add $piece to the end, so the left, of $string.
>
> mb_concat($str, $piece):
> If mb_append() and mb_prepend() are too hard to understand by users, 
> mb_concat() would be pretty much simpler and will act in the same way.
I would insert:
mb_replace($str, $search, $replace):
Complementary to str_replace and less simpler than preg_replace or 
preg_filter (and with better performance because it does not need to 
compile regex to automata etc.).

> mb_pad($length, $piece, $end):
> Pretty much like strpad but with multi-bytes considering.
> Note that, like mb_append and mb_prepend, we do not speak about left 
> and right, but start and end.
>
> mb_get_direction($str):
> Return LTR or RTL. For one char, it is “easy”. For a string, it is 
> more complex because we can have embedding directions (a string with 
> many directions).
>
> mb_get_char($code) or mb_chr($code):
> Get Unicode character from a code-point/decimal value representation.
>
> mb_get_code($char) or mb_ord($char):
> Get code-point/decimal value representation from of Unicode character.
>
> Finally, it would be great if `mb_substr()` could consider text 
> direction (or create a new dedicated function?). For example, 
> mb_substr($str, 0, 1) will return the first char from the start (and 
> not the left as it is implemented now).
>
> I think it could help developers to create nice libraries without 
> requiring a lot of skills in Unicode.
> Thoughts?
>
> Best regards :-).

Recently, I crafted a String object that support most of the 
propositions written here. Please, see 
https://github.com/hoaproject/String (file String.php). The code is very 
simple (it implies that it do not need a lot of work to implement it in 
PHP I hope). Some interesting methods:
     • append;
     • prepend;
     • pad;
     • getIterator (equivalent of str_split);
     • \ArrayAccess methods (with support of out-of-bound indexes);
     • getByteAt (with support of out-of-bound indexes);
     • getCharDirection;
     • fromCode;
     • toCode.

Hope this helps :-),
Best regards.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/

---

## #63232 — Moshe Samuel — 2012-09-24T18:10:29+00:00

URL: https://externals.io/message/62072#63232

It would be nice, especially if "Hello"->replace("ll","xx") could work
regular expressions.



> 
>
> > […]
> approach to scalar operations would be great. "hello,
> world"->replace('world', 'php'); would be so much nicer than str_replace
> and forgetting the argument order. This is a little off-topic, though.
>
> --
> Andrew Faulds
> http://ajf.me/

---

## #63235 — Ivan Enderlin @ Hoa — 2012-09-25T08:03:39+00:00

URL: https://externals.io/message/62072#63235

> 
> > […]
> I would love such a library to be built into PHP. A more consistent 
> approach to scalar operations would be great. "hello, 
> world"->replace('world', 'php'); would be so much nicer than 
> str_replace and forgetting the argument order. This is a little 
> off-topic, though.
Yup, it is off-topic. We can add new functions in one hand and 
“autoboxing” scalars into OOP style in another hand.

-- 
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://disc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/
