PHP Function to Replace a (I)Th-Position Character

PHP function to replace a (i)th-position character

$str    = 'bar';
$str[1] = 'A';
echo $str; // prints bAr

or you could use the library function substr_replace as:

$str = substr_replace($str,$char,$pos,1);

Replace a specific character at a specific position with some other character

Use substr_replace()

$string = "Hello";
$char = "a";
$pos = 1;
$str = substr_replace($string,$char,$pos,1);

echo $str; // Hallo

PHP to replace 2 character at nth position in an string

Try this -

$url="http://localhost/abc/index.php/en/news/title/new-version-of-goldendict-with-dzongkha";

echo $str = substr_replace($url,'dz',31,2);

Hope this will help you.

Easiest way to replace all characters in even positions in a string.

It is easily done with a regular expression:

echo preg_replace('/(.)./', '$1 ', $str);

The dot matches a character. Every second character is replaced with a space.

Best way to replace/remove a specific character when it appears between 2 character sequences

So it was a good thing I asked for the output, because initially I had something else. Many people would use regular expressions here, but I often find those difficult to work with, so I took a more basic approach:

function extractWantedStuff($input)
{
$output = [];
$sections = explode('""', $input);
$changeThisSection = false;
foreach ($sections as $section) {
if ($changeThisSection) {
$section = str_replace(',', '', $section);
}
$output[] = $section;
$changeThisSection = !$changeThisSection;
}
return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

The output would be:

,""Word1 Word2"" and another thing ,""Word3 Word4""

See: Example code

Slightly more optimized, by removing the $changeThisSection boolean:

function extractWantedStuff($input)
{
$output = [];
$sections = explode('""', $input);
foreach ($sections as $key => $section) {
if ($key % 2 != 0) { // is $key uneven?
$section = str_replace(',', '', $section);
}
$output[] = $section;
}
return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

See: Example code

And further optimized, by removing the $output array:

function extractWantedStuff($string)
{
$sections = explode('""', $string);
foreach ($sections as $key => $section) {
if ($key % 2 != 0) {
$sections[$key] = str_replace(',', '', $section);
}
}
return implode('""', $sections);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

See: Example code

How to replace the characters in fixed positions in PHP?

use

substr_replace()

like

substr_replace($string, '****', 4 , 4);

read more :

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

Replace last occurrence of a string in a string

You can use this function:

function str_lreplace($search, $replace, $subject)
{
$pos = strrpos($subject, $search);

if($pos !== false)
{
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}

return $subject;
}

How to replace or insert a character on multiple locations on a string using substr_replace, can I add more than one position on the function?

Use preg_replace:

$input = "A fits B Fits C Fits D";
$output = preg_replace("/\b([A-Z]+)(?=\s)/", "$1,", $input);
echo $input . "\n" . $output;

This prints:

A fits B Fits C Fits D
A, fits B, Fits C, Fits D

Here is an explanation of the regex pattern:

\b([A-Z]+) match AND capture one or more uppercase letters, which are preceded
by a word boundary
(?=\s) then assert that what follows is a space; this prevents a final
letter from being assigned a comma, should the input end in a letter

Then, we replace with $1, the captured letter(s), followed by a comma.

Edit:

For your recent edit, you may use:

$input = "White Fits Black Fits Red Fits Blue";
$output = preg_replace("/\b(?=\s+Fits\b)/", ",", $input);
echo $input . "\n" . $output;

This prints:

White Fits Black Fits Red Fits Blue
White, Fits Black, Fits Red, Fits Blue


Related Topics



Leave a reply



Submit