[Last Call] Learn how to a build a cloud-first strategyRegister Now


need a php function that insert a white space

Posted on 2006-05-07
Medium Priority
Last Modified: 2013-12-13
I need a php function that allows me to insert a white space into some string
after 10 characters for example ..
so any help would be greatly appreciated.
Question by:Ameerh24
  • 2
  • 2
LVL 17

Expert Comment

ID: 16625476
Hi Ameerh24,

Just use a regex to replace the first 10 characters with the first ten characters with a space.
Or even better, use str_replace() to replace the first 10 characters with the first 10 characters with a space..
Need an example?

Joe P
LVL 17

Expert Comment

ID: 16625502
Further still you could use substr() to get a string with the first 10 characters.  Do to that what you wish.  What exactly is it that you want to do after the first 10 characters?

Joe P
LVL 17

Expert Comment

by:Cem Türk
ID: 16625504
try this one;

function insertSpace($myStr) {
if(strlen($mystr)<10) { return $myStr; }
else { return  substr($myStr, 0,10) . " " . substr($myStr,10,strlen($mystr)-10); }
LVL 17

Accepted Solution

Cem Türk earned 1500 total points
ID: 16625522
here is more customizable version;

function insertStr($myStr,$n,$strToInsert) {
if(strlen($mystr)<$n) { return $myStr; }
else { return  substr($myStr, 0,$n) . $strToInsert . substr($myStr,$n,strlen($mystr)-$n); }

example call ;

$result=insertStr("abcdefghijklmnop",10," ");

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question