brosjr Posted December 7, 2010 Share Posted December 7, 2010 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 https://forums.phpfreaks.com/topic/220922-find-replace-and-increase/ Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 Which "many solutions" have you tried so far? Link to comment https://forums.phpfreaks.com/topic/220922-find-replace-and-increase/#findComment-1143983 Share on other sites More sharing options...
brosjr Posted December 7, 2010 Author Share Posted December 7, 2010 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 https://forums.phpfreaks.com/topic/220922-find-replace-and-increase/#findComment-1143984 Share on other sites More sharing options...
sasa Posted December 8, 2010 Share Posted December 8, 2010 <?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 https://forums.phpfreaks.com/topic/220922-find-replace-and-increase/#findComment-1144293 Share on other sites More sharing options...
brosjr Posted January 4, 2011 Author Share Posted January 4, 2011 sasa, I really appreciate your attention. Thank you. Link to comment https://forums.phpfreaks.com/topic/220922-find-replace-and-increase/#findComment-1154415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.