PHP and REGEX: replace content in a div

Posted on 2009-04-22
Last Modified: 2012-05-06
In the previous question ( I wanted to know how to replace everything in the <head>. This is working perfectly.

Now I need to find the below and replace it in my html:

<div id="news_archive_head_title">Directory</div>

I tried using the example but it seems to be going further than the next div (it's replace more code). Thanks.
Question by:level9wizard
    LVL 8

    Accepted Solution

    don't you think it would be a lot more easy to actually store the different content in variables and replace them under certain conditions ?
    $divContent = "Directory";
    if($myConditionIsMet) { # put your condition here
        $divContent = "newDivContent" ;
    <div id="news_archive_head_title"><?php echo $divContent; ?></div>

    Open in new window

    LVL 19

    Expert Comment

    can you post your code and an example (before and after html) that didn't work?
    LVL 74

    Assisted Solution

    by:käµfm³d 👽

        $html = preg_replace('|\<div\sid="news_archive_head_title"\>.*?\</div\>|siu',$newdiv,$html);

    what you're looking for?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    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 …

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now