
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678. -- Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790 <http://www.facebook.com/ErickNjenga> <http://www.twitter.com/ErickNjenga>

Do a loop and every 4 steps use stringreplace.... On Mon, Oct 25, 2010 at 8:12 PM, Erick Njenga <eriknjenga@gmail.com> wrote:
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678.
--
Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790
<http://www.facebook.com/ErickNjenga><http://www.twitter.com/ErickNjenga>
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

you locate a string position by using arrays $fourth = $str[3]; then use the str_replace function On Mon, Oct 25, 2010 at 8:12 PM, Erick Njenga <eriknjenga@gmail.com> wrote:
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678.
--
Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790
<http://www.facebook.com/ErickNjenga><http://www.twitter.com/ErickNjenga>
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Try this..... $orig_string = "123456789"; $insert_string = "-"; $position = "4"; $newstring=substr_replace($orig_string, $insert_string, $position, 0); echo $newstring; Regards, Julianne From: eriknjenga@gmail.com Date: Mon, 25 Oct 2010 20:12:49 +0300 To: skunkworks@lists.my.co.ke Subject: [Skunkworks] PHP String Manipulation Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678. -- Thanks and Regards,Erick Njenga Nyachwaya, M: +254-725-008-790 _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

What i would personally do is use regular expression replacement Your search regex will be as follows .{4} That will match every four characters The replacement regex will be as follows $&- That will replace each match i.e. each four characters with the match and then add a dash after On Tue, Oct 26, 2010 at 9:45 AM, julianne anyim <julia.alma@hotmail.com>wrote:
Try this.....
$orig_string = "123456789"; $insert_string = "-"; $position = "4"; $newstring=substr_replace($orig_string, $insert_string, $position, 0); echo $newstring;
Regards, Julianne
------------------------------ From: eriknjenga@gmail.com Date: Mon, 25 Oct 2010 20:12:49 +0300 To: skunkworks@lists.my.co.ke Subject: [Skunkworks] PHP String Manipulation
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678.
--
Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790
<http://www.facebook.com/ErickNjenga><http://www.twitter.com/ErickNjenga>
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Try this var string_name = /^[0-9]{4}-[0-9]{4}$/; My guess is you are trying to validate a phone number? On Tue, Oct 26, 2010 at 10:03 AM, Rad! <conradakunga@gmail.com> wrote:
What i would personally do is use regular expression replacement
Your search regex will be as follows
.{4}
That will match every four characters
The replacement regex will be as follows
$&-
That will replace each match i.e. each four characters with the match and then add a dash after
On Tue, Oct 26, 2010 at 9:45 AM, julianne anyim <julia.alma@hotmail.com>wrote:
Try this.....
$orig_string = "123456789"; $insert_string = "-"; $position = "4"; $newstring=substr_replace($orig_string, $insert_string, $position, 0); echo $newstring;
Regards, Julianne
------------------------------ From: eriknjenga@gmail.com Date: Mon, 25 Oct 2010 20:12:49 +0300 To: skunkworks@lists.my.co.ke Subject: [Skunkworks] PHP String Manipulation
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678.
--
Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790
<http://www.facebook.com/ErickNjenga><http://www.twitter.com/ErickNjenga>
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94------------ Other services @ http://my.co.ke
_______________________________________________
Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

+1 for regex $str = "12345678901234567890"; echo trim(preg_replace("/(.{4})/", "\\1-", $str), "-"); On 10/26/2010 10:03 AM, Rad! wrote:
What i would personally do is use regular expression replacement
Your search regex will be as follows
.{4}
That will match every four characters
The replacement regex will be as follows
$&-
That will replace each match i.e. each four characters with the match and then add a dash after
On Tue, Oct 26, 2010 at 9:45 AM, julianne anyim <julia.alma@hotmail.com <mailto:julia.alma@hotmail.com>> wrote:
Try this.....
$orig_string = "123456789"; $insert_string = "-"; $position = "4"; $newstring=substr_replace($orig_string, $insert_string, $position, 0); echo $newstring;
Regards, Julianne
------------------------------------------------------------------------ From: eriknjenga@gmail.com <mailto:eriknjenga@gmail.com> Date: Mon, 25 Oct 2010 20:12:49 +0300 To: skunkworks@lists.my.co.ke <mailto:skunkworks@lists.my.co.ke> Subject: [Skunkworks] PHP String Manipulation
Anyone got an idea of an internal PHP function that would enable me to insert a symbol e.g. '-' after every 4 characters in a String? e.g. 12345678 becomes 1234-5678.

Yes, Regex is the best choice On Tue, Oct 26, 2010 at 12:00 PM, Dennis Kioko <dmbuvi@gmail.com> wrote:
I also support using regex _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Solomon Kariri, Software Developer, Cell: +254736 729 450 Skype: solomonkariri

Thanks all. I ended up using Jonas' regexp. Worked like a charm. :-) @Ouya, I was trying to add the hyphen to the numbers so that they are read correctly in excel without truncation. On Tue, Oct 26, 2010 at 2:35 PM, solomon kariri <solomonkariri@gmail.com>wrote:
Yes, Regex is the best choice
On Tue, Oct 26, 2010 at 12:00 PM, Dennis Kioko <dmbuvi@gmail.com> wrote:
I also support using regex _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Solomon Kariri,
Software Developer, Cell: +254736 729 450 Skype: solomonkariri
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Thanks and Regards, Erick Njenga Nyachwaya, M: +254-725-008-790 <http://www.facebook.com/ErickNjenga> <http://www.twitter.com/ErickNjenga>
participants (9)
-
bernard kioko
-
Dennis Kioko
-
Erick Njenga
-
Jonas | Lamu Software
-
julianne anyim
-
Ouya Obunga
-
Rad!
-
solomon kariri
-
Theus Owuor