문제

I have a string in PHP like this:

<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-69 current_page_item"><a href="/transport-quote-step-2/">Transport Quote Step 2</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>

Now I want to locate LI element that contains class "page-item-69" and I need to delete whole LI element so I can get output like this:

<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>
도움이 되었습니까?

해결책

In today's PHP world, the preferred method here would be using PHP's DOM model. Sadly, I am still learning this myself and do not have a good grasp on this. For what it's worth, though, here is my stab at it. (There may be an easier/better way to do this.)

$string = '<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-69 current_page_item"><a href="/transport-quote-step-2/">Transport Quote Step 2</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>';

$dom_document = new DOMDocument(); // CREATE A NEW DOCUMENT
$dom_document->loadHTML($string); // LOAD THE STRING INTO THE DOCUMENT
$lis = $dom_document->getElementsByTagName('li'); // PULL OUT THE LIS OUT OF THE DOCUMENT

// LOOP THROUGH EACH LI
foreach ($lis AS $li) {

    // IF WE FIND page-item-69, DELETE THE ITEM
    if (preg_match('/page-item-69/', $li->getAttribute('class'), $m)) {
        $li->parentNode->removeChild($li);
    }

}

$new_string_2 = $dom_document->saveHTML(); // WRITE THE CHANGES TO A STRING

print $new_string_2;

That does seem to work for me, but since I am old school, here is how one would go about it using REGEX:

$string = '<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-69 current_page_item"><a href="/transport-quote-step-2/">Transport Quote Step 2</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>';

$string = preg_replace('~(\s*)<li class="page_item page-item-69.*?</li>~i', '', $string);

print $string;

This will output the following:

<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>

다른 팁

IF you need to do this server side, then here is a good PHP example of how to remove everything under a specific tag PHP: Strip a specific tag from HTML string?

Here is a code example of how I would handle this with JQUERY.

<!DOCTYPE html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>

        <scripts>
        $(document).ready(function(){
          /* DELETE */
          $(".page-item-69").remove();
          /* Otherwise hide */
          $(".page-item-69").hide();
        });
        </scripts>


    </head>
    <body>
    <?php 
    /* YOUR PHP CODE */
    echo YOUR_STRING . "/n";
    ?>

    </body>
</html>

This will help you:

$str = '<nav role="navigation" class="main-navigation" id="site-navigation">
  <li class="page_item page-item-2"><a href="/sample-page/">Sample Page</a></li>
  <li class="page_item page-item-69 current_page_item"><a href="/transport-quote-step-2/">Transport Quote Step 2</a></li>
  <li class="page_item page-item-70"><a href="/transport-quote-step-3/">Transport Quote Step 3</a></li>
  <li class="page_item page-item-71"><a href="/transport-quote-step-4/">Transport Quote Step 4</a></li>
</nav>';

$doc = new DOMDocument();
@$doc->loadHTML($str);

foreach ($doc->getElementsByTagName('li') as $tag) {
    if (strpos($tag->getAttribute('class'), 'page-item-69') !== false) {
        $tag->parentNode->removeChild($tag);
    }
}

$doc->removeChild($doc->firstChild);
$doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);
$str = $doc->saveHTML();

echo $str;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top