Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!