SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Find and Replace

    Hi guys

    I need to find and replace some code in a variable. Basically I will be searching $var for this:

    HTML Code:
    <div class="content">
    ...body...
    </div>
    And I want to replace it with:

    HTML Code:
    <div class="content">
     <div class="tl"></div>
     <div class="tm"></div>
     <div class="tr"></div>
     <div class="ml"></div>
     <div class="mm">
     ...body...
     </div>
     <div class="mr"></div>
     <div class="bl"></div>
     <div class="bm"</div>
     <div class="br"></div>
    </div>
    Or something to that effect. I assume I need to use regular expressions but in all honesty, that's always been lost on me. Can anyone help? Thanks guys

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Code:
    $html = 
    '<div class="content">
    ...body...
    </div>';
    
    $replacement =  
    '$1
     <div class="tl"></div>
     <div class="tm"></div>
     <div class="tr"></div>
     <div class="ml"></div>
     <div class="mm">
     $2
     </div>
     <div class="mr"></div>
     <div class="bl"></div>
     <div class="bm"></div>
     <div class="br"></div>
     $3';
    
    
    $regex = '/(\<div\sclass\=\"content\"\>)(.*?)(\<\/div\>)/s';
    
    echo preg_replace($regex,$replacement,$html);
    ?

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your going to have to parse the string. If ...body... is not any code you can easily use str_replace.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question

    oddz, that looks like what I'm after, thanks

    imaginethis, I can't use str_replace can I? I don't want it to replace every </div> with the numerous divs that are there, only the ones that pertain to this particular div class.

    I was actually thinking about putting a code in to every <div> and matching </div>, something that the system can parse to match them up (making sure there are open and close tags) and use that to find and replace too as my worry with the regex is that any divs within the content divs will be closed with these extras and that would break the design completely.

    So, if I did something like:

    HTML Code:
    <div class="content" code="123456">
    ...body...
    </div code="123456">
    and replace opening <div class="content"> tags with:

    HTML Code:
    <div class="content">
        <div class="tl"></div>
        <div class="tm"></div>
        <div class="tr"></div>
        <div class="ml"></div>
        <div class="mm">
    and closing </div> tags with:

    HTML Code:
        </div>
        <div class="mr"></div>
        <div class="bl"></div>
        <div class="bm"</div>
        <div class="br"></div>
    </div>
    If I had this however:

    HTML Code:
    <div class="content">
        <div class="somethingelse">
        ...body...
        </div>
        <div class="somethingelse">
        ...body...
        </div>
    </div>
    Using the regex above, wouldn't that result in something like this:

    HTML Code:
    <div class="content">
        <div class="tl"></div>
        <div class="tm"></div>
        <div class="tr"></div>
        <div class="ml"></div>
        <div class="mm">
        <div class="somethingelse">
        ...body...
        </div>
        <div class="mr"></div>
        <div class="bl"></div>
        <div class="bm"</div>
        <div class="br"></div>
    </div>
        <div class="somethingelse">
        ...body...
        </div>
    </div>
    which is clearly wrong

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Antnee View Post
    imaginethis, I can't use str_replace can I? I don't want it to replace every </div> with the numerous divs that are there, only the ones that pertain to this particular div class.
    He meant if "...body..." is a keyword then you can simply run a str_replace to get your desired result.

    If "...body..." is simply representing n amount of random code, then you can't.

    Your definition wasn't exactly crystal clear.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  6. #6
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can just do str_replace <div class="content" code="123456"> and </div code="123456"> if thats all you need.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  7. #7
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your ......body..... including div tags ( called nested tags ) this solutions are not work. you must write a function for this or google it

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You'd probably be better off using XML parsing here.

    Why? Because it knows when a tag starts and finishes, whilst RegEx can't.

    Something to the effect of
    PHP Code:
    $doc = new DomDocument;
    $doc->LoadHTML('<div id="content">
    ...body...
    </div>'
    );
    $element $doc->getElementById('content');
    $element->nodeValue =  '<div class="tl"></div>
     <div class="tm"></div>
     <div class="tr"></div>
     <div class="ml"></div>
     <div class="mm">
     ' 
    $element->nodeValue '
     </div>
     <div class="mr"></div>
     <div class="bl"></div>
     <div class="bm"</div>
     <div class="br"></div>'

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It'd be something like:
    PHP Code:
    $doc = new DomDocument();
    $load $doc->LoadHTML('
    <html>
        <body>
            <div class="content">...body...</div>
        </body>
    </html>'
    );
    $elements $doc->getElementsByTagName('div');
    foreach(
    $elements as $element){
        if(
    $element->getAttribute('class') == 'content'){
            
    $element->nodeValue =  '<div class="tl"></div>
                <div class="tm"></div>
                <div class="tr"></div>
                <div class="ml"></div>
                <div class="mm">' 
    $element->nodeValue '</div>
                <div class="mr"></div>
                <div class="bl"></div>
                <div class="bm"</div>
                <div class="br"></div>'
    ;
        }

    The issue here is that the html set as the value gets htmlentities applied automatically, so this (evidently) won't work. But if anyone who's used XML with PHP has any suggestions, fire ahead!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    It's not pretty, and may well be overkill for your particular needs, but here's an example of taking a snippet of HTML and wrapping the contents of specific elements with some extra elements (like your example). It could (should) probably be refactored quite a bit, but what do you expect for five minutes on a lazy Sunday afternoon?

    PHP Code:
    $doc = new DOMDocument();

    // Load in our source HTML snippet
    $source $doc->createDocumentFragment();
    $source->appendXML(trim('
    <html>
        <body>
            <div class="content">...body...</div>
        </body>
    </html>
    '
    ));
    $doc->appendChild($source);

    // Find div.content elements
    $xpath = new DOMXPath($doc);
    $elements $xpath->query('//div[@class="content"]');
    foreach(
    $elements as $element)
    {
        
    // Find innerHTML - U G L Y - but you can make it pretty
        
    $inner_html '';
        
    $bits $doc->createDocumentFragment();
        foreach (
    $element->childNodes as $child)
            
    $bits->appendChild($child->cloneNode(true));
        
    $tmp = new DOMDocument();
        
    $tmp->appendChild($tmp->importNode($bitstrue));
        
    $inner_html rtrim($tmp->saveHTML());
        unset(
    $tmp$bits);
        
        
        
    // Prepare replacement HTML string
        
    $html '
                 <div class="tl"></div>
                 <div class="tm"></div>
                 <div class="tr"></div>
                 <div class="ml"></div>
                 <div class="mm">'
    .$inner_html.'</div>
                 <div class="mr"></div>
                 <div class="bl"></div>
                 <div class="bm"></div>
                 <div class="br"></div>
                '
    ;
        
        
    // Create replacement HTML object
        
    $replacement $doc->createDocumentFragment();
        
    $replacement->appendXML($html);

        
    // Remove original innerHTML
        
    while ($element->hasChildNodes())
        {
            
    $element->removeChild($element->firstChild);
        }
        
        
    // Add in our replacement HTML
        
    $element->appendChild($replacement);
    }

    echo 
    $doc->saveHTML(); 
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you all. I shall give your suggestions a whiz. Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •