Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
PHP Do not convert between 2 tags

I have a string. With "pre" tag inside. I don't want to replace the texts in this pre tag. How can I do that?

<?php
$str="
<b>test</b>
<pre>
<b>test</b>
</pre>
<b>test</b>
";
function htmltobb($str){
    $str=preg_replace('(<b>)','[b]',$str);
    $str=preg_replace('(</b>)','[/b]',$str);
    return $str;
}
echo htmltobb($str);
?>

result:

[b]test[/b]
<pre>
[b]test[/b]
</pre>
[b]test[/b]

what i want?

[b]test[/b]
<pre>
<b>test</b> <-- this line not replaced
</pre>
[b]test[/b]

thank you for all answers

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

A messy approach could be taking everything between pre tags, HTML encoding it, parse everything else, then decode the entities back to characters... this assumes regexs are required and you already have read about their pitfalls.

$str="
<b>test</b>
<pre>
<b>test</b>
</pre>
<b>test</b>
";
echo preg_replace_callback(htmlspecialchars('~(?s)<pre>.*?</pre>~'), function($match) { return htmlspecialchars_decode($match[0]);}, preg_replace('~<(/?)b>~','[$1b]', preg_replace_callback('~(?s)<pre>.*?</pre>~', function($match) { return htmlspecialchars($match[0]);}, $str)));

https://3v4l.org/UZbc2

Start with:

<b>test</b>
<pre>
<b>test</b>
</pre>
<b>test</b>

First run converts to:

<b>test</b>
&lt;pre&gt;
&lt;b&gt;test&lt;/b&gt;
&lt;/pre&gt;
<b>test</b>

Second converts to:

[b]test[/b]
&lt;pre&gt;
&lt;b&gt;test&lt;/b&gt;
&lt;/pre&gt;
[b]test[/b]

Third puts it back as intended:

[b]test[/b]
<pre>
<b>test</b>
</pre>
[b]test[/b]
over 4 years ago · Santiago Trujillo Denunciar

0

You need to go cutting your string, do the replace when not inside a pre, and not when yes inside a pre.

$str="
<b>test</b>
<pre>
<b>test</b>
</pre>
<b>test</b>
";

echo htmltobb($str);

function htmltobb($str){    
    $aux = '';
    $posB = 0;
    while (($posA = mb_strpos($str, '<pre>', $posB)) !== false) {
        $aux .= preg_replace(['(<b>)', '(</b>)'], ['[b]', '[/b]'], mb_substr($str, 0, $posA));
        $posB = mb_strpos($str, '</pre>', $posA);
        $aux .= mb_substr($str, $posA, $posB - $posA);
    }
    $aux .= preg_replace(['(<b>)', '(</b>)'], ['[b]', '[/b]'], mb_substr($str, $posB));

    return $aux;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda