如何 highlight_string 使用 php 标签前的表单中的内容
How to highlight_string content from a form within tags pre using php
我的内容是从带有 ajax 的所见即所得表单发布的,其中代码放置在标签
之间
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>
发布内容后,在显示已发布的内容之前,在后端我想使用 php 函数 highlight_string 突出显示标签之间的所有内容 <pre>
是否它们 类 是否像上面的示例一样添加到标签前。
发布内容后,所见即所得编辑器将内容转为 htmlentities,因此内容变为
<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>
所以基本上在后端我想用 <pre>highlight_string ('content')</pre>
替换 <pre>...content ...</pre>
。
为了实现这一点,我首先尝试检索 <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);
}
##########################################
}
但是当我这样做时我得到:
int(2) Notice: Array to string conversion.
如何使用 php highlight_string 标签 <pre>
和 </pre>
中的表单内容 php ?
嗯,
那怎么样:
<?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;
}
首先,您的 (.*?)
不会匹配任何跨行的内容,因为 .
不匹配换行符,除非您指定 s
标志。
尝试:
<?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;
我的内容是从带有 ajax 的所见即所得表单发布的,其中代码放置在标签
之间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>
发布内容后,在显示已发布的内容之前,在后端我想使用 php 函数 highlight_string 突出显示标签之间的所有内容 <pre>
是否它们 类 是否像上面的示例一样添加到标签前。
发布内容后,所见即所得编辑器将内容转为 htmlentities,因此内容变为
<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>
所以基本上在后端我想用 <pre>highlight_string ('content')</pre>
替换 <pre>...content ...</pre>
。
为了实现这一点,我首先尝试检索 <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);
}
##########################################
}
但是当我这样做时我得到:
int(2) Notice: Array to string conversion.
如何使用 php highlight_string 标签 <pre>
和 </pre>
中的表单内容 php ?
嗯, 那怎么样:
<?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;
}
首先,您的 (.*?)
不会匹配任何跨行的内容,因为 .
不匹配换行符,除非您指定 s
标志。
尝试:
<?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;