lfernando Posted January 17, 2011 Share Posted January 17, 2011 Hello, I have a $string that has a bunch of DIVs like this Some text here <DIV id=test_20_here> Hello </div> <DIV id=test_5_here> How are you </div> Some text here <DIV id=test_115_here> Good Day </div> I need to find all theses DIVs and delete them. I still need the text inside (Hello, How are you, Good day), but the DIV tag needs to go. I figured i could use a str_replace("<DIV id=test_".$count."_here","",$string), However I dont know the numbers in between the "test_" and the "_here" so i dont know how many times to do the str_replace. Surely there's an easier way anyways? Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/ Share on other sites More sharing options...
AbraCadaver Posted January 17, 2011 Share Posted January 17, 2011 $new = preg_replace('~<div[^>]*>(.*?)</div>~is', '$1', $old); Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/#findComment-1160916 Share on other sites More sharing options...
Psycho Posted January 17, 2011 Share Posted January 17, 2011 $new = preg_replace('~<div[^>]*>(.*?)</div>~is', '$1', $old); Just to elaborate on AbraCadaver's excellent solution, you would just run that one function against the entire string ($old) with multiple divs. The line will gnerate the variable $new with all the divs removed, but the contents of the divs will remain. However, looking at your original post it is unclear if there are other divs (i.e. that don't have an ID in the format "id=test_XX_here") which you DO NOT want replaced. If that is the case, then a small modification is in order: $new = preg_replace('~<div id=test_[^>]*>(.*?)</div>~is', '$1', $old); That will remove the opening and closing div tags where the opening div starts with "<div id=test_". Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/#findComment-1160930 Share on other sites More sharing options...
lfernando Posted January 17, 2011 Author Share Posted January 17, 2011 Thank you both for this reply. It's exactly what I was looking for. I know very little (zeroactually) about reg exp. I am handling a big number of $strings and im not sure if there are other divs in some, but in any case you've helped me tremendously. Thank you! Fernando Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/#findComment-1160957 Share on other sites More sharing options...
lfernando Posted January 17, 2011 Author Share Posted January 17, 2011 Hmm - maybe spoke too quickly Neither of these worked. What am doing wrong? This is what my string ACTUALLY looks like: $old="...following in a couple of color modes.</P><UL><LI id=1565_10_tab1><DIV id=1565_9_tab1>Select tool; -> Tool can be selected without error <LI id=1565_1_tab1>Clic..." $new = preg_replace('~<div[^>]*>(.*?)</div>~is', '$1', $old); echo "<textarea>$new </textarea>"; $new is the same as $old Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/#findComment-1160969 Share on other sites More sharing options...
lfernando Posted January 17, 2011 Author Share Posted January 17, 2011 Nevermind! found my error!! Quote Link to comment https://forums.phpfreaks.com/topic/224746-find-substring-with-unknown-character-test_x_here/#findComment-1160974 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.