Reputation: 432
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 /> </p> <pre><code><span style="color: #000000"> <br /> &lt;?php echo "Jesus is the way the truth and the life and Jesus is Lord and He is God"; ?&gt; <br /></span> </code></pre> <p> <br /> stuf after content <br /> </p> <pre><code><span style="color: #000000"> <br /> &lt;?php $jesusis="Lord"; echo "Another content code"; ?&gt; <br /></span> </code></pre> <p> <br /> </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
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 />
</p>
<pre class="brush: applescript; fontsize: 100; first-line: 1; ">
<?php echo "Jesus is the way the truth and the life and Jesus is Lord and He is God"; ?>
</pre>
<p>
<br />
stuf after content
<br />
</p>
<pre class="brush: applescript; fontsize: 100; first-line: 1; ">
<?php $jesusis="Lord"; echo "Another content code"; ?>
</pre>
<p>
<br />
</p>';
$text = preg_replace_callback(
'~<pre.*?>(.*?)</pre>~is',
function($matches) {
return('<pre>'.highlight_string(html_entity_decode($matches[1]), true).'</pre>');
},
$content
);
echo $text;
Upvotes: 2
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;
}
Upvotes: 0