Jump to content

Find, replace and increase


brosjr

Recommended Posts

Good morning all,

 

Please, I hope someone could help me to solve this, I searched all over and I can't figure out how to do it.

 

I have a text with many paragraphs started with <P> tags on it and I need to replace then to <P class=X>, where X has to be increased from 0, like <P class=0>, <P class=1>, <P class=2>, and so on until the last <P> tag.

 

I tried many solutions and I just got replaced the <P> tags for <P class=0>, <P class=0>, <P class=0>.

 

Really thankx.

Danilo Jr.

Link to comment
Share on other sites

:D Searching and trying a little bit more, I found a solution that fitted weel for my application:

 

$count = count(explode("<P>", $text));

 

$pieces= explode("<P>", $text);

$t = 0;

while ($t < $count){

$teste.= substr_replace($pieces[$t],"<P class=$t>",0,0);

$t = $t+1;

}

 

echo $teste;

 

Salathe, I'm new on this stuff, and I tryed many solutions before I got this.

 

Thankx

Danilo Jr

Link to comment
Share on other sites

<?php
$test ='<P>bla</p><P  >bla</p><P>bla</p><P>bla</p><P>bla</p>';
function my_cnt($a){
    static $i = 0;
    return "<P class='".$i++."'".$a[1];
}
echo preg_replace_callback('/<P(\s|>)/', 'my_cnt', $test);
?>

Link to comment
Share on other sites

  • 4 weeks later...
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.