How to strip out html with php inside a given div tag?

Posted on 2008-11-08
Medium Priority
Last Modified: 2012-08-14

I would like to replace some text found in a particular div tag.

I managed to do this myself but had some limitations - cannot replace data found in a div tag when it is enclosed in another tag. eg: <div id="replace"><strong>5336</strong></div>

If the <strong> tag was not there the below php function would have replaced the content with $appendContent.
$newContent = preg_replace('#<div id="replace">[^<]*</div>#', '<div id="replace">'.$appendContent."</div>", $oldContent);

Open in new window

Question by:Rassac
  • 2
  • 2
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22913741
Why not just simplify the test?

$newContent = preg_replace('#<div id="replace">(.+)</div>#', '<div id="replace">'.$appendContent."</div>", $oldContent);

Author Comment

ID: 22914461
Thanks! that worked perfectly well! Do you know where I can find our what these type of symbols mean "(.+)" please?
LVL 34

Accepted Solution

Beverley Portlock earned 2000 total points
ID: 22915838
The dot (.) means "any character", the plus means "one or more times" so .+ means any character occurring 1 or more times.  Regular expressions can be highly complex and difficult to read or set up. Keep them as basic and simple as possible. Start with these articles




Author Closing Comment

ID: 31514699
GREAT! Thanks :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question