str_replace the 1st <tr>

<?php
$myText="[COLOR="Blue"]preTable[/COLOR]&nbsp;[COLOR="blue"]text[/COLOR]<br />
[COLOR="Red"]<table>[/COLOR]<br />
&nbsp;&nbsp;[B][COLOR="red"]<tr>[/COLOR][/B]<br />
&nbsp;&nbsp;&nbsp;&nbsp;[COLOR="red"]<td>[/COLOR][COLOR="Blue"]text1[/COLOR]&nbsp;[COLOR="blue"]in[/COLOR]&nbsp;[COLOR="Blue"]1st[/COLOR]&nbsp;[COLOR="blue"]td[/COLOR]&nbsp;[COLOR="blue"]of[/COLOR]&nbsp;[COLOR="blue"]1st[/COLOR]&nbsp;[COLOR="blue"]tr[/COLOR][COLOR="red"]</td>[/COLOR]<br />
&nbsp;&nbsp;&nbsp;&nbsp;[COLOR="red"]<td>[/COLOR][COLOR="blue"]text2[/COLOR]&nbsp;[COLOR="blue"]in[/COLOR]&nbsp;[COLOR="Blue"]2nd[/COLOR]&nbsp;[COLOR="blue"]td[/COLOR]&nbsp;[COLOR="Blue"]of[/COLOR]&nbsp;[COLOR="Blue"]1st[/COLOR]&nbsp;[COLOR="Blue"]tr[/COLOR][COLOR="red"]</td>[/COLOR]<br />
&nbsp;&nbsp;[COLOR="red"]</tr>[/COLOR]<br />
&nbsp;&nbsp;[COLOR="red"]<tr>[/COLOR]<br />
&nbsp;&nbsp;&nbsp;&nbsp;[COLOR="red"]<td>[/COLOR][COLOR="blue"]text3[/COLOR]&nbsp;[COLOR="blue"]in[/COLOR]&nbsp;[COLOR="blue"]1st[/COLOR]&nbsp;[COLOR="blue"]td[/COLOR]&nbsp;[COLOR="Blue"]of[/COLOR]&nbsp;[COLOR="blue"]2nd[/COLOR]&nbsp;[COLOR="blue"]tr[/COLOR][COLOR="red"]</td>[/COLOR]<br />
&nbsp;&nbsp;&nbsp;&nbsp;[COLOR="red"]<td>[/COLOR][COLOR="blue"]text4[/COLOR]&nbsp;[COLOR="Blue"]in[/COLOR]&nbsp;[COLOR="blue"]2nd[/COLOR]&nbsp;[COLOR="Blue"]td[/COLOR]&nbsp;[COLOR="Blue"]of[/COLOR]&nbsp;[COLOR="blue"]2nd[/COLOR]&nbsp;[COLOR="Blue"]tr[/COLOR][COLOR="red"]</td>[/COLOR]<br />
&nbsp;&nbsp;[COLOR="red"]</tr>[/COLOR]<br />
[COLOR="Red"]</table>[/COLOR]<br />
[COLOR="Blue"]postTable[/COLOR]&nbsp;[COLOR="blue"]text[/COLOR]";

?>

Let’s suppose I have $myText like the above.

The code below changes all “<tr>” to “openTR”.

$myText=str_replace("<tr>","openTR",$myText);

I like to change only the 1st <tr> to “openTR”

You could use preg_replace() which has a parameter where you can set a limit on the number of replacements.

There are 5 examples in the page of the manual.
Which example is the most related to my original question?

None. :sick:

Do you mean that I should read all from the example1 to the example5 and others?

Sure, go wild. The only really important point, for replacing a string once, is to read the description (and placement) of the limit parameter.

What is the function of the limit parameter?
Where is the example code of the limit parameter?

It dictates the maximum possible replacements and defaults to -1, which means to use no limit.

An example is:


$subject = 'a a a a';
echo preg_replace('/a/', 'b', $subject);    // Outputs: b b b b
echo preg_replace('/a/', 'b', $subject, 3); // Outputs: b b b a
echo preg_replace('/a/', 'b', $subject, 2); // Outputs: b b a a
echo preg_replace('/a/', 'b', $subject, 1); // Outputs: b a a a

Is the code below the solution of my original post?

$myText=preg_replace("/<tr>/","openTR",$myText,1);

By the way,
I guess that reg in preg_replace means regular expression.
Then, what is the meaning of p in preg_replace?

The p is for PERL perl regularexpression