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

Posted on 2008-11-08
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
    LVL 34

    Expert Comment

    by:Beverley Portlock
    Why not just simplify the test?

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

    Author Comment

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

    Accepted Solution

    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

    GREAT! Thanks :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now