'How replace text with DOMDocument

Need change letter "a" to "1" and "e" to "2"

This is approximately html, in fact it is more nested

<body>
  <p>
    <span>sppan</span>
    <a href="#">link</a>
    some text
  </p>
  <p>
    another text
  </p>
</body>

expected output

<body>
  <p>
    <span>spp1n</span>
    <a href="#">link</a>
    some t2xt
  </p>
  <p>
    anoth2r t2xt
  </p>
</body>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source