John Max
John Max

Reputation: 432

How to highlight_string content from a form within tags pre using php

I have content that is posted from a WYSIWYG form with ajax where codes are placed between tags

stuf before content <pre class="brush: php; light: true; collapse: true; fontsize: 500; first-line: 1; ">
    <?php  echo "Jesus is the way the truth and the life and Jesus is Lord and He is God"; ?>  </pre> stuf after content  <pre class="brush: php; light: true; collapse: true; fontsize: 500; first-line: 1; ">
    <?php $jesusis='Lord';  echo "Another content code"; ?>  </pre>

Once the content is posted, before displaying what has been posted, on the backend I want to use the php function highlight_string to highlight every thing between the tags <pre> whether they are classes added to the tags pre like in the example above or not.

Once the content is posted, the WYSIWYG editor turns the content to htmlentities so the content becomes

<p> stuf before content <br /> &nbsp; </p> <pre><code><span style="color: #000000"> <br />&nbsp;&nbsp;&nbsp;&nbsp;&amp;lt;?php&nbsp;echo&nbsp;"Jesus&nbsp;is&nbsp;the&nbsp;way&nbsp;the&nbsp;truth&nbsp;and&nbsp;the&nbsp;life&nbsp;and&nbsp;Jesus&nbsp;is&nbsp;Lord&nbsp;and&nbsp;He&nbsp;is&nbsp;God";&nbsp;?&amp;gt; <br /></span> </code></pre> <p> &nbsp;&nbsp;&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp; stuf after content &nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </p> <pre><code><span style="color: #000000"> <br />&nbsp;&nbsp;&nbsp;&nbsp;&amp;lt;?php&nbsp;$jesusis="Lord";&nbsp;echo&nbsp;"Another&nbsp;content&nbsp;code";&nbsp;?&amp;gt; <br /></span> </code></pre> <p> <br /> &nbsp;&nbsp;&nbsp; </p>

So basically on the back end I want to replace <pre>...content ...</pre> with <pre>highlight_string ('content')</pre>.

In order to achieve that I first of all tried to retrieve every match between <pre>

preg_match('~<pre.*?>(.*?)</pre>~i', $content, $matches, PREG_OFFSET_CAPTURE);

//Count number of matches
$compter_matches = count($matches); 

//if we have at least one match 
if($compter_matches>0)
    {

    ###############################
        for($i=0; $i<$compter_matches; $i++)
        {
            $a_remplacer = $matches[$i];
            $replacement = '<pre>'.highlight_string($a_remplacer, true).'</pre>';

$note = preg_replace('~<pre.*?>(.*?)</pre>~i', $replacement, $content);



        }
    ##########################################  
    }   

But when i do that i get:

int(2) Notice: Array to string conversion.

How to highlight_string content from a form within tags <pre> and </pre> using php ?

Upvotes: 0

Views: 232

Answers (2)

Booboo
Booboo

Reputation: 44203

First of all your (.*?) will not match anything that spans across lines because . does not match the newline character unless you specify the s flag.

Try:

<?php

$content = '<p>
    stuf before content
    <br />
    &nbsp;
</p>
<pre class="brush: applescript; fontsize: 100; first-line: 1; ">
    &lt;?php echo "Jesus is the way the truth and the life and Jesus is Lord and He is God"; ?&gt;
</pre>
<p>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    &nbsp;&nbsp;&nbsp; stuf after content &nbsp;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
</p>
<pre class="brush: applescript; fontsize: 100; first-line: 1; ">
    &lt;?php $jesusis="Lord"; echo "Another content code"; ?&gt;
</pre>
<p>
    <br />
    &nbsp;&nbsp;&nbsp;
</p>';

$text = preg_replace_callback(
    '~<pre.*?>(.*?)</pre>~is',
    function($matches) {
        return('<pre>'.highlight_string(html_entity_decode($matches[1]), true).'</pre>');
    },
    $content
    );
echo $text;

See Demo

Upvotes: 2

VirCom
VirCom

Reputation: 3622

Hmm, What about that:

<?php
$content = '<pre>sadfasdfsadf</pre>';

$matches = array();

preg_match('~<pre.*?>(.*?)</pre>~i', $content, $matches, PREG_OFFSET_CAPTURE);

//Count number of matches
$compter_matches = count($matches); 

//if we have at least one match 
if($compter_matches>0)
{
    for($i = 0; $i < $compter_matches; $i += 2) {
        $a_remplacer = $matches[$i][0];
        $replacement = '<pre>'.highlight_string($a_remplacer[$i+1][0], true).'</pre>';
        $note = str_replace($a_remplacer[$i], $replacement, $content);
    }

    echo $note;
}   

online test

Upvotes: 0

Related Questions