Jump to content

Find substring with unknown character? ( test_X_here)


lfernando

Recommended Posts

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!

Link to comment
Share on other sites

$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_".

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :shrug:

Link to comment
Share on other sites

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.