www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] How I remove a href link in between div class?

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    resolved [RESOLVED] How I remove a href link in between div class?

    I'm trying figure out how remove a href link between div class on php script but tough is:

    normal display:

    <div class="showtimes"> <a href="#">10:00am</a> <span class="ghost">|</span> <a href="#>10:30am</a> <span class="ghost">|</span> <a href="#>11:00am</a> </div>

    I need php script remove a href link change to like:

    <?php

    $page = preg_replace('/<div class\=[\"]showtimes[\"]\>(.*?)<\/div>/si','\\1',$page);

    ?>

    <div class="showtimes"> 10:00am <span class="ghost">|</span> 10:30am <span class="ghost">|</span> 11:00am </div>


    How I do remove a href link only in between div class?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,922
    PHP Code:
    <?php
    $page 
    = <<<EOD
    <div class="showtimes"> <a href="#">10:00am</a> <span class="ghost">|</span>
    <a href="#">10:30am</a> <span class="ghost">|</span> <a href="#">11:00am</a> </div>
    EOD;

    echo 
    "<h2>Before:</h2>\n$page";

    function 
    foo($matches)
    {
       
    $result  $matches[1];
       
    $result .= preg_replace('#<a\s[^>]*>(.*?)</a>#is''$1'$matches[2]);
       
    $result .= $matches[3];
       return 
    $result;
    }
    $regexp '#(<div\s[^>]*class=[\'"]showtimes[\'"][^>]*>)(.*?)(</div>)#is';
    $page preg_replace_callback($regexp'foo'$page);

    echo 
    "<h2>After:</h2>\n$page";
    However, that solution depends on the HTML being properly formatted, and nasty things can happen if it is not, such as when I copied and pasted your HTML that was missing the closing quotes in a couple of the href attributes.

    A more robust solution would be to use the DOM functions to find each div of class "showtimes" and then within them finding each each <a> element, going through contortions to get the text value of each <a>, removing the <a> element and inserting the text, but I'm way too lazy to do that for you right now.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2011
    Posts
    2

    Perfect!

    It's worked! Perfect. Thank you very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles