Answer the question
In order to leave comments, you need to log in
What is the regular expression for replacing the code tag with pre code?
In the example, the code tag is replaced by src.
there is a task to find all < src > ... < /src > tags
and enclose in < pre> < src class=''> ... < /src >< /pre>
the src tag can have the lang attribute of the
regular expression
$sText = preg_replace("/<src * lang=(.*?)>(.*)<\/src\>/is", "<pre><src class=$1>$2</src></pre>", $sText);
Answer the question
In order to leave comments, you need to log in
$sText = preg_replace("#^<src lang=(.*?)>(.*?)</src>$#ism", "<pre><src class=$1>$2</src></pre>", $sText);
There are wonderful classes for working with XML, and there is no need to torture regular expressions.
$doc = new DomDocument('1.0', 'utf-8');
$doc->loadXml($text);
$xp = new DomXPath($doc);
foreach ($xp->query('//src') as $src_node) {
$pre_node = $doc->createElement('pre');
$pre_node->appendChild($src_node);
$src_node->parentNode->replaceChild($pre_node, $src_node);
};
$new_text = $doc->saveXML();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question