need a php function that insert a white space

Posted on 2006-05-07
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
    LVL 17

    Expert Comment

    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

    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

    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

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now