skip to Main Content

I want to change tag name inside some text for example;

<div class="col-sm-12 col-md-6 col-lg-4 float-left">
  <div class="service-one__single">
    <span class="service-one__dot-1">BLA BLA 1</span>
    <span class="service-one__dot-2">BLA BLA 2</span>
    <span class="service-one__dot-3">BLA BLA 3</span>
  </div>
</div>

I want to replace <span> tags with <p> dynamically. After change i want to see codes like below;

<div class="col-sm-12 col-md-6 col-lg-4 float-left">
  <div class="service-one__single">
    <p class="service-one__dot-1">BLA BLA 1</p>
    <p class="service-one__dot-2">BLA BLA 2</p>
    <p class="service-one__dot-3">BLA BLA 3</p>
  </div>
</div>

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution... Here is the solution;

    $text = '<div class="col-sm-12 col-md-6 col-lg-4 float-left">
                        <div class="service-one__single">
                            <span class="service-one__dot-1">BLA BLA 1</span>
                            <span class="service-one__dot-2">BLA BLA 2</span>
                            <span class="service-one__dot-3">BLA BLA 3</span>
                        </div>
                    </div>';
    
    $text = preg_replace("/<spans(.+?)>(.+?)</span>/is", "<p $1>$2</p>", $text);
    

  2. I used a PHP tool called DOMDocument to analyze HTML. It locates all <span> elements within a <div> with a particular class.

    Then, it substitutes each one with a <p> element containing the same text.

    Lastly, it displays the modified HTML.

    <?php
    // HTML content
    $html = '<div class="col-sm-12 col-md-6 col-lg-4 float-left">
                <div class="service-one__single">
                    <span class="service-one__dot-1">BLA BLA 1</span>
                    <span class="service-one__dot-2">BLA BLA 2</span>
                    <span class="service-one__dot-3">BLA BLA 3</span>
                </div>
            </div>';
    
    // Create a DOMDocument
    $dom = new DOMDocument();
    // Load HTML content into the DOMDocument
    $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
    // Create a DOMXPath instance
    $xpath = new DOMXPath($dom);
    
    // Query for all <span> elements within the specified class
    $spanNodes = $xpath->query("//div[@class='service-one__single']/span");
    
    // Iterate through each <span> element
    foreach ($spanNodes as $span) {
        // Create a new <p> element
        $p = $dom->createElement('p');
        // Set the content of the <p> element to the content of the <span> element
        $p->nodeValue = $span->nodeValue;
        // Replace the <span> element with the newly created <p> element
        $span->parentNode->replaceChild($p, $span);
    }
    
    // Get the updated HTML content
    $newHtml = $dom->saveHTML();
    
    // Output the updated HTML content
    echo $newHtml;
    ?>
    

    Output

    <div class="col-sm-12 col-md-6 col-lg-4 float-left">
                <div class="service-one__single">
                    <p>BLA BLA 1</p>
                    <p>BLA BLA 2</p>
                    <p>BLA BLA 3</p>
                </div>
            </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search