preg match - Replacing text using a loop in PHP -


i trying loop through every [footnote] , replace html. here sample text:

hello [footnote], how [footnote], [footnote]? 

and using preg_match_all create count:

$match_count = preg_match_all("/\[footnote]/", $content); 

i use count loop, find , replace text appropriate html:

for ($i=0; $i < $match_count; $i++) {     $new_content = str_replace('[footnote]', "<span class='footnote'>$i</span>", $content); } 

however, after, when echo $new_content; each [footnote] has same number, 2:

<span class="footnote">2</span> <span class="footnote">2</span> <span class="footnote">2</span> 

would know why number not incrementing? want

<span class="footnote">1</span> <span class="footnote">2</span> <span class="footnote">3</span> 

str_replace replaces @ once, need preg_replace supports $limit (=number of replacements make):

$content = "hello [footnote], how [footnote], [footnote]?";  $i = 0; {     $i++;     $content = preg_replace('~\[footnote\]~', "<span>$i</span>", $content, 1, $count); } while($count);  print $content; 

note 5th parameter, $count makes counting code superfluous - keep replacing until no more replacement made.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -