PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Jan 25, 2020 6:46 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: insert dash into string
PostPosted: Thu Apr 10, 2008 1:05 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Nov 01, 2006 6:44 pm
Posts: 105
I have string that has 9 digits in it. I want to insert a dash after the 5th number. Is there a string function I can use to accomplish this?


Top
 Profile  
 
PostPosted: Thu Apr 10, 2008 1:08 pm 
Offline
Forum Newbie

Joined: Sun Apr 06, 2008 3:43 pm
Posts: 11


Top
 Profile  
 
PostPosted: Thu Apr 10, 2008 1:22 pm 
Offline
Forum Contributor
User avatar

Joined: Wed Nov 01, 2006 6:44 pm
Posts: 105
:)


Top
 Profile  
 
PostPosted: Thu Apr 10, 2008 3:40 pm 
Offline
Site Administrator
User avatar

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
Syntax: [ Download ] [ Hide ]
<?php
$str = '932348787';
$str = substr($str, 0, 5) . '-' . substr($str, 5);
?>


Top
 Profile  
 
PostPosted: Thu Apr 10, 2008 5:47 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto


Top
 Profile  
 
PostPosted: Thu Apr 10, 2008 6:17 pm 
Offline
Site Administrator
User avatar

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
Well then let's convolute this baby...

Syntax: [ Download ] [ Hide ]
<?php
$new = '';
for ($i = 0; $i < strlen($str); $i++) {
    $new .= $str[$i];
    if ($i == 5) {
        $new .= '-';
    }
}
 
echo $new;
?>


I like your better Jcart. :wink:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group