Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert space after # of characters 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How would I go about a code that would insert a space after, say 5 characters, so if someone does 555555555555, it would show as 55555 55555 55?
 
use preg_replace

/([a-zA-Z0-9]{5})/sU" as pattern and then replace by the same thing with a space

:)

this is one way Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
or else
Code:
function addspace($string){
  $count=0;
  $new="";
  for($i=0;$string[i];$i++){
     if ($string[i]>='a' && $string[i]<='z' || $string[i]>='A' && $string[i]<='Z' || $string[i]>='0' && $string[i]<='9'){
         $new.=$string[i];
         $count++;
         if ($count==5){
            $new.=&quot; &quot;;
            $count=0;
         }
      }

  }
  return $new;
}

i did not tested the function, but i presume it wikll work fine.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Pah ! easy :)

<?php
$text = &quot;555555555555555555555555555555555555&quot;;
$newtext = wordwrap( $text, 5, &quot; &quot;, 1);

echo &quot;$newtext\n&quot;;

?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
You got a star cause i was lazy to look in the manual :) Everyday we learn new functions :) Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thanks man :) - I just happened to have needed the same thing a while ago.

Theres a lot of C/perl and API compatable functions I didnt realise existed, not well documented but well worth a read :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top