anarchoi Posted June 12, 2009 Share Posted June 12, 2009 i use this small piece of code to replace ALL "[NOUVEAU_CHAPITRE]" and number replacements example: with the following string: ABC [NOUVEAU_CHAPITRE]DEF[/NOUVEAU_CHAPITRE] GHI [sOUS_CHAPITRE]JKL[/sOUS_CHAPITRE] MNO [NOUVEAU_CHAPITRE]PQR[/NOUVEAU_CHAPITRE] STU [sOUS_CHAPITRE]VWX[/sOUS_CHAPITRE] YZ [sOUS_CHAPITRE]123[/sOUS_CHAPITRE] the result would be ABC ITEM 1 : DEF xxxx GHI [sOUS_CHAPITRE]JKL[/sOUS_CHAPITRE] MNO ITEM 2 : PQR xxxx STU [sOUS_CHAPITRE]VWX[/sOUS_CHAPITRE] YZ [sOUS_CHAPITRE]123[/sOUS_CHAPITRE] here is the code i use: $text = preg_replace_callback( '/\[NOUVEAU_CHAPITRE\]/i', create_function( '$matches', '$GLOBALS["xnum"] = (isset($GLOBALS["xnum"]) ? $GLOBALS["xnum"]+1 : 1); return "<br>NUMBER " . $GLOBALS["xnum"] . " : ";' ), $text ); $one = "[/NOUVEAU_CHAPITRE]"; $two = "xxxx<br>"; $striptext = str_replace($one, $two, $text); now i would also like to replace all of the tags [sOUS_CHAPITRE] anywhere in the text, just like the other tag but i don't want to replace it with the same thing here is an example of the result i want: ABC ITEM 1 : DEF xxxx GHI ITEM 1.1 : JKL zzzz MNO ITEM 2 : PQR xxxx STU ITEM 2.1 : VWX zzzz YZ ITEM 2.2 : 123 zzzz it's the first time i use this function, and i don't get how i could do those 2 replacements at same time with correct numbers... I have tryed many ways and i always get errors thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/161982-solved-preg_replace_callback-and-callback-function/ Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 Do you mean something like this <?php /* ABC ITEM 1 : DEF xxxx GHI ITEM 1.1 : JKL zzzz MNO ITEM 2 : PQR xxxx STU ITEM 2.1 : VWX zzzz YZ ITEM 2.2 : 123 zzzz*/ $str ="ABC [NOUVEAU_CHAPITRE]DEF[/NOUVEAU_CHAPITRE] GHI [sOUS_CHAPITRE]JKL[/sOUS_CHAPITRE] MNO [NOUVEAU_CHAPITRE]PQR[/NOUVEAU_CHAPITRE] STU [sOUS_CHAPITRE]VWX[/sOUS_CHAPITRE] YZ [sOUS_CHAPITRE]123[/sOUS_CHAPITRE]"; $text = preg_replace_callback('%(\[(.*?)\])(.*?)(\[/\2\])%sim',"Clean",$str); echo $text; function Clean($matches) { static $counter, $sub; if($matches[1]=="[NOUVEAU_CHAPITRE]") { $counter++; $sub=0; $tag1 = "$counter"; $tag2 = "xxxx"; }else{ $sub++; $tag1 = "$counter.$sub"; $tag2 = "zzzz"; } return "ITEM $tag1 : {$matches[3]}: $tag\n"; } ?> ABC ITEM 1 : DEF: GHI ITEM 1.1 : JKL: MNO ITEM 2 : PQR: STU ITEM 2.1 : VWX: YZ ITEM 2.2 : 123: Quote Link to comment https://forums.phpfreaks.com/topic/161982-solved-preg_replace_callback-and-callback-function/#findComment-854687 Share on other sites More sharing options...
anarchoi Posted June 12, 2009 Author Share Posted June 12, 2009 yes, it works perfectly thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/161982-solved-preg_replace_callback-and-callback-function/#findComment-854732 Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 welcome Can you click topic solved please (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/161982-solved-preg_replace_callback-and-callback-function/#findComment-854737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.