-1

I'm trying to replace the full stop on the nth position in a string. I just want to remove the nth full stop from the string and not all.

My input is a paragraph with n full stops. In this example I'm having 4 full stops.

My name is Resheil. I'm developing in PHP. I have used mysql for my application. I want to replace the full stop on the nth position in php.

Supposing this statement I want to replace 3rd full stop in this string with a string.

Replacing the third dot with ". Here I had a full stop."

EXPECTED OUTPUT :

My name is Resheil. I'm developing in PHP. I have used mysql for my application. Here I had a full stop. I want to replace the full stop on the nth position in php.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Resheil Agarwal
  • 155
  • 1
  • 11
  • Please share your `input` , `expected output` and `code` which you have tried. – Sahil Gulati Aug 27 '17 at 07:36
  • I tried the other solutions available on stackoverflow but they seem to replace the last or the first character or the replace all the full stops. – Resheil Agarwal Aug 27 '17 at 08:00
  • @ResheilAgarwal So let me ask directly: Are you only ever replacing a fullstop? or might you want to replace `the`? This determines how flexible the code needs to be. Please edit your question to include your failed coding attempts -- this will help your question to be re-opened. – mickmackusa Aug 28 '17 at 00:17
  • I just want to replace 3rd full stop with a string that's it. Nothing else. I don't know why is this down voted and my question was edited. I narrowed it down by some one made an edit to it. There are no array of strings, just 1 string with 4 fullstops. Out of which I need to replace the 3rd full stop. The green tick thing perfectly worked for me. – Resheil Agarwal Aug 28 '17 at 00:21
  • Better advice at [Replace the Nth occurrence of char in a string with a new substring](https://stackoverflow.com/q/65192020/2943403) – mickmackusa May 07 '23 at 08:49

2 Answers2

1

Hope this post will be helpful. This post will remove nth substring from a string.

Solution 1:

Try this code snippet here

$stringToSearch=".";
$string="My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php.";
preg_match_all("/".  preg_quote($stringToSearch)."/", $string,$matches,PREG_OFFSET_CAPTURE);
$nthStringtoRemove=3;
if(isset($matches[0][$nthStringtoRemove-1]))
{
    echo $string=substr_replace($string, "", $matches[0][$nthStringtoRemove-1][1], strlen($stringToSearch));
}

Solution 2:

Try this code snippet here

<?php

$string="My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php.";

$counter=$offset=$start=0;
$nthStringtoRemove=3;//nth position to replace
$stringToSearch=".";//string to search and replace.
while($offset = strpos($string, $stringToSearch,$start))
{
    $start=$offset+1;
    $counter++;
    if($counter==$nthStringtoRemove)
    {
        $string=substr_replace($string, "", $offset, strlen($stringToSearch));
        break;
    }
}
echo $string;
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

This task only requires one function call. The preg_replace() match non-dot substrings then dots $n-1 times, then on the nth occurrence it will handle the targeted dot.

Assuming these variables:

$n=4;
$strings=["First. Second.Third.",
          "First. Second.Third.  Fourth.",
          "First. Second. Third. Fourth. Fifth.",
          "First. Second.Third.  Fourth. Fifth. Sixth. Seventh. Eighth."
         ];

Method #1

(Keep everything except for the 4th dot): (Demo) (Regex Pattern/Replacement Demo)

$strings=preg_replace('/((?:[^.]*?\.){'.($n-1).'}[^.]*)\.(.*)/','$1$2',$strings);
var_export($strings);

Output:

array (
  0 => 'First. Second.Third.',
  1 => 'First. Second.Third.  Fourth',
  2 => 'First. Second. Third. Fourth Fifth.',
  3 => 'First. Second.Third.  Fourth Fifth. Sixth. Seventh. Eighth.',
)

Method #2

(Replace only the 4th dot with an empty string) (Demo) (Regex Pattern/Replacement Demo)

$strings=preg_replace('/(?:[^.]*\.){'.($n-1).'}[^.]*?\K\./','',$strings,1);  // note the last parameter `1` so that it doesn't match the eighth dot
var_export($strings);
// same results as first method

The above methods will work the same on your input string as it does on my array of strings.

$n=4;
$string="My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php.";

$string=preg_replace('/((?:[^.]*?\.){'.($n-1).'}[^.]*)\.(.*)/','$1$2',$string);
echo $string;
// or
$string=preg_replace('/(?:[^.]*\.){'.($n-1).'}[^.]*?\K\./','',$string,1);
echo $string;
// output:
// My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php

If you'd like to see a non-regex method, this works by "bumping right" the starting point of strpos() on each iteration of the for loop. If the process locates a 4th dot, it displays the two substrings on either side of it. If no 4th dot is found, the full string is displayed.

Method #3

(Demo)

$n=4;
$string="My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php.";
for($pos=-1,$i=0; $i<$n; ++$i){
    if(($pos=strpos($string,'.',$pos+1))===false){  // locate and store offset
        break;  // no qualifying dot to remove
    }
}
echo $pos===false?$string:substr($string,0,$pos),substr($string,$pos+1);  // qualifying portions of $string

Output:

My name is Resheil. I'm developing in PHP. I have used mysql for my application.  I want to replace the full stop on the nth position in php
Community
  • 1
  • 1
mickmackusa
  • 43,625
  • 12
  • 83
  • 136