- substr: documentation ( source)
- substr_count: documentation ( source)
- strpos: documentation ( source)
<?php
$string = "[vc_tab [vc_tab [vc_tab [vc_tab [vc_tab [vc_tab";
$active = 3;
$needle = "[vc_tab";
$replace = '[vc_tab active="true"';
if( substr_count( $string, $needle) < $active) {
throw new Exception("There aren't enough needles in the string to do this replacement");
}
$count = 0; $index = 0;
while( $count++ < $active) {
$index = strpos( $string, $needle, $index) + strlen( $needle);
}
$result = substr( $string, 0, $index - strlen( $needle)) . $replace . substr( $string, $index);
echo "Index: ", $index, " ", $result;