preg substitute

Dear Sir/Madam

in following string

Pls note the text i want to embed is after 6th occurrence of " (double quote) in following string

I am unable to do it

pls help

In short
following string

will be

Pls note the text i want to embed is after 6th occurrence of "

Need to know what attempt you have made before I will assist,

$string = ‘The quick brown fox jumps over the lazy “dog”?abcd.’;
$patterns = array();
$patterns[0] = ‘/quick/’;
$patterns[1] = ‘/brown/’;
$patterns[2] = ‘/fox/’;
$patterns[3] = ‘/lazy/’;

$replacements = array();
$replacements[3] = ‘?autoplay=1’;
$replacements[2] = ‘bear’;
$replacements[1] = ‘black’;
$replacements[0] = ‘slow’;
echo “
attempt 1 :
”.preg_replace($patterns, $replacements, $string);

result

attempt 1 :
The ?autoplay=1 bear black jumps over the slow “dog”?abcd.


Attempt 2

$code=‘abcd?’;
$patterns = array();
$patterns[0] = ‘[?]’;

$replacements = array();

$replacements[0] = ‘?Autoplay=1’;
echo “
attempt 2 :
”. preg_replace($patterns, $replacements, $code);

Result

attempt 2 :
abcd?Autoplay=1


Attempt 3

echo ‘Attempt 3’;

$code=‘abcd " aaaa "cccc’;
$patterns = array();
$patterns[0] = ‘["]’;
/$patterns[1] = ‘/youtube.com/’;
$patterns[2] = ‘/frameborder/’;
/
$replacements = array();
/$replacements[2] = ‘bear’;
$replacements[1] = ‘frameborderxx’;
/
$replacements[0] = ‘?Autoplay=1’;
echo “
attempt 3 :
”. preg_replace($patterns, $replacements, $code);

Result

attempt 3 :
abcd ?Autoplay=1 aaaa ?Autoplay=1cccc


echo ‘Attempt 4’;

$code=‘abcd " aaaa "cccc’;
$patterns = array();
$patterns[0] = ‘["{1,}]’;

$replacements = array();

$replacements[0] = ‘?Autoplay=1’;
echo “
attempt 4 :
”. preg_replace($patterns, $replacements, $code);

Result
attempt 4 :
abcd ?Autoplay=1 aaaa ?Autoplay=1cccc

I want to replace only for 2nd occurence
cannot find syntax for replaceing " of 2nd occurence

You aren’t demonstrating that you know what the issue it.

Your attempt is a literal copy paste from sources on how preg_replace and str_replace work.

Your:

[php]

will be

[/php]

is the exact same thing.

Dear Sir / Mdam

Really Sorry for this

will be

Your statement
Your attempt is a literal copy paste from sources on how preg_replace and str_replace work.
is true
But
couldnot find how I can replace exactly the sixth occurence

Sponsor our Newsletter | Privacy Policy | Terms of Service