Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4120470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:13:35+00:00 2026-05-20T23:13:35+00:00

Example of markup: <p> a paragraph </p> <pre lang=html> &lt;p&gt; a paragraph &lt;/p&gt; </pre>

  • 0

Example of markup:

<p> a paragraph </p>
<pre lang="html">
  &lt;p&gt; a paragraph &lt;/p&gt;
</pre>
<code lang="html">
  &lt;p&gt; a paragraph &lt;/p&gt;
</code>

How can I select all the stuff between <pre>,</pre>,<code>,</code> and run a function on it? Trough this function I need to pass 3 arguments: the part of the string that’s selected (&lt;p&gt; a paragraph &lt;/p&gt;), the container type (pre or code), and the parameters of the container (like lang="html").

The function should change the selected part of the string based on the other 2 parameters (if it’s relevant I want run the GeShi highlighter on it), then replace the contents of the original string with it, including the container. Something like:

<p> a paragraph </p>
<div class="html pre">
  &lt;p&gt; a paragraph &lt;/p&gt;
</div>
<div class="html code">
  &lt;p&gt; a paragraph &lt;/p&gt;
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:13:35+00:00Added an answer on May 20, 2026 at 11:13 pm

    I think it should go like this:

    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    
    $elements = $xpath->query('//pre | //code');
    

    In some cases (e.g.: if you use getElementsByTagName instead of XPath), you will need to operate on an array to get the proper behaviour (see this question), so you need to copy the nodes to an array. I’ll do it for this example:

    $array = array();
    foreach ($elements as $element) {
        $array[] = $element;
    }
    
    foreach ($array as $element) {
        $tag = $element->tagName;
        $content = $element->textContent;
        $lang = $element->getAttribute('lang');
        $new_content = my_function($tag, $content, $lang);
    
        $new_element = $dom->createElement('div');
        $new_element->setAttribute('class', "$tag $lang");
        $new_element->nodeValue = $new_content;
        $element->parentNode->replaceChild($new_element, $element);
    }
    

    Of course, in the example above, the my_function is undefined. But it should give you a good idea on the howto.

    Note that this won’t work on nested elements, like these:

    <pre lang="html">
      <p>some nested element</p>
      &lt;p&gt; a paragraph &lt;/p&gt;
    </pre>
    

    If you want to work on nested elements, use a function to get the innerHTML instead of using $element->textContent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are all those markup languages programming languages? For example XML or HTML are programming
Been searching the net for an example of how to convert HTML string markup
Does Django have any template tags to generate common HTML markup? For example, I
I have this markup, <a href=http://www.example.com>Hiya<span class=delete>Delete</span></a> And my js, $('.delete').click(function() { window.location =
i got an object with contents of html markup in it, for example: string
I want to crop a part of an HTML markup with Inline-JavaScript included. Example:
for example i have the markup <div> <img src=# alt=/> <img src=# alt=/> <img
How does one go about catching exceptions from using controls in markup? For example,
Example: select ename from emp where hiredate = todate('01/05/81','dd/mm/yy') and select ename from emp
I have, for example, markup like this <div id=content> <p>Here is some wonderful text,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.