Jump to content

Recommended Posts

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

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:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.