473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Str replace for a particular position leaving white space

I just wan to replace x in a string. I want to replace 4 th postion to 9 position with x. There is no replace for a white space.

String = John Robert Michelle
Output = Johx xxxxrt Michelle

White space shud not count ..

Count only the characters not the white space
Feb 27 '13 #1
1 1531
Atli
5,058 Expert 4TB
This function should let you do that.

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Replaces a specific part of a string with a another string, repeated to fill in the indicated
  3.  * position of the original string. If a position originally has a letter that exists in the
  4.  * $ignoreLetters string, it will be ignored.
  5.  * @param string $string The text to modify
  6.  * @param string $replacement The string that should replace the given position.
  7.  * @param int [$startIndex=0] The first index that should be replaced
  8.  * @param int [$length=PHP_INT_MAX] The total amount of letters to replace.
  9.  * @param string [$ignoreLetters=" "] A string of letters that should be ignored.
  10.  * @return string
  11.  */
  12. function substr_repeat_ignore($string, $replacement, $startIndex=0, $length=PHP_INT_MAX, $ignoreLetters=" ") {
  13.     if (!$string || $length <= 0 || $startIndex >= strlen($string)) {
  14.         return $string;
  15.     }
  16.     if ($startIndex < 0) {
  17.         $startIndex = 0;
  18.     }
  19.  
  20.     /*
  21.      * Iterates over the given position within the $string, replacing each position that does not
  22.      * have a character that exists inside the $ignoreLetters string with the next character that
  23.      * has not been used in the $replacement. Once $replacement has been used fully, it goes back
  24.      * to the start of the $replacement.
  25.      */
  26.     $currentPosition = $startIndex;
  27.     $endIndex = $startIndex + $length;
  28.     $replacementIndex = 0;
  29.     while ($currentPosition < $endIndex && $currentPosition < strlen($string)) {
  30.         if (strpos($ignoreLetters, $string[$currentPosition]) === false) {
  31.             $string[$currentPosition] = $replacement[$replacementIndex];
  32.             ++$replacementIndex;
  33.             if ($replacementIndex >= strlen($replacement)) {
  34.                 $replacementIndex = 0;
  35.             }
  36.         }
  37.         ++$currentPosition;
  38.     }
  39.  
  40.     return $string;
  41. }
  42.  
Mar 2 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Grumble | last post by:
Hello all, I want to read lines from a text file, where each line has the following syntax: token1:token2:token3 There could be white space between tokens and ':'
1
by: Generale Cluster | last post by:
hello, I've made a template which has the layout I want, but there is an undesired white space between the left elements and the right column. How can I remove it? Thank you Bye!! Here's the...
3
by: Simon Dean | last post by:
Hello! Im looking to try and get into CSS for formatting my sites. Seems like a nice premise, and should allow easier design for the future. But gees, Im having a hard time of it. I would...
1
by: CC | last post by:
Hi, We have an asp.net app we are building for invoice inquiry. The problem we are having is that we can have anywhere from 1-100 (or more) line items and then we have a dozen or so data...
0
by: Tom Jastrzebski | last post by:
Hello everybody, It looks like this is a known problem, but I found no solution. Deserialization of DataSet object from XML does not preserve white space. The same code executed under .Net...
22
by: Brent | last post by:
I want to paste a lengthly text document into a web page and have it retain all of whitespace and line breaks. I first tried the <pretag but that didn't work because it doesn't wrap the text. I...
12
by: JA | last post by:
Is there a way to remove all the white space in the fields? I have been using Find-and-replace - looking for 2 or 3 or 4 or 10 spaces and replacing them with none. I don't want to replace single...
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
5
tharden3
by: tharden3 | last post by:
How do I remove white space around an icon? I have been using some helpful icon pics from google images to spice up a website that I'm making. In many instances though, the icon is not square, but...
2
by: Jibran | last post by:
I need some help with extra spaces in HTML form. There is a big white space appearing at the center of the HTML form that I am designing even though there is no <br> tags been used: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.