0

Just wondering if it is possible to change a character in a string with more than 1 character. Here's my failed attempt:

<?
$string = abcde;
echo "character 2: ". $string[2] . "\n";
$string[2] = "X";
echo "character 2: ". $string[2] . "\n";
$string[2] = "moreThanOneChar";
echo "character 2: ". $string[2] . "\n";
echo "whole string: $string";
?>

output:

character 2: c
character 2: X
character 2: m      
whole string: abmde   // <-- This should be abmoreThanOneCharde

http://codepad.org/e2dDkH37

dukevin
  • 22,384
  • 36
  • 82
  • 111
  • When using a string indexer (i.e. $string[2]) you are telling the interpreter you want a 'reference' to the second character of the string. This is a single character. Likewise, when you are setting a value to this indexer, you can only set a single character; therefore, you need a string replacement function. – Chris Hutchinson Mar 17 '12 at 02:17

3 Answers3

2
function replaceInString($str, $pos, $substring)
{
     $part1 = substr($str, 0, -$pos);
     $part2 = substr($str, -$pos + 1);
     return $part1 . $substring . $part2;
}
QuantumBlack
  • 1,549
  • 11
  • 27
1

You can solve this using a regular expression (regex) replacement mechanism:

$string = preg_replace('/X/', 'moreThanOneChar', $string);

Note; however, that this will replace every instance of 'X' with 'moreThanOneChar'. If you want to limit this to only the first time, you can pass a limit as a fourth parameter:

$string = preg_replace('/X/', 'moreThanOneChar', $string, 1);

This can be easily expanded to replace phrases:

$string = 'Hello, man!';
$string = preg_replace('/man/', 'person', $string);

echo $string;

Output:

Hello, person!

Review the preg_replace command for more information:

http://php.net/manual/en/function.preg-replace.php

Chris Hutchinson
  • 9,082
  • 3
  • 27
  • 33
  • doesn't it replace all the matches in the string? I mean in string "popeye" when you try to replace 3. char, you are not actually replacing all the "p"'s in the string. – Taha Paksu Mar 17 '12 at 02:37
  • It replaces as many matches as you specify, which is by default all of them, but you can set the limit in the fourth parameter. – Chris Hutchinson Mar 17 '12 at 02:46
1

From the docs:

The string in PHP is implemented as an array of bytes and an integer indicating the length of the buffer.

So, while a string can be treated like an array of characters, it is not the same as other types of arrays where each element can be changed into a new data type. Each element of a "string array" will always be a 1-byte character.

When you try to tell PHP "make this third character a string of other characters", PHP responds by saying "ok, but I'm going to only use the first character from the new string".

Edit

Some clarification after seeing the other answers. I took your question to mean that you want to continue to access characters by location after the change.

For example:

$string = 'abcde';
echo $string[2];  //echoes c
$string[2] = 'fghi';
echo $string[2]:  //echoes fghi

This is simply not possible the way you describe.

If you really need functionality like that, convert the string to an array of characters and then operate on the array elements.

Community
  • 1
  • 1
Farray
  • 8,290
  • 3
  • 33
  • 37