我想在使用nl2br时忽略以下标记。我有标准的pre,还有一些风格的特定pre标签。当我执行此操作时,会在<br>
标记内添加<pre>
个标记。
string = "Here is some text
<pre>
<xml>
<item></item>
<name></name>
</xml>
</pre>
That includes new lines and carriage returns
what can i do to add <p> and <br> tags but not to the text below inside
the <pre> tags </pre>.
<pre class="brush: csharp; title: ;" title="">
public MainPage()
{
// select the culture (de, it, fr, tr are supported)
var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc
// this controls the UI strings
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
// this controls number and date formatting (optional in this sample)
//System.Threading.Thread.CurrentThread.CurrentCulture = ci;
// initialize the component<br>
InitializeComponent();
}
</pre>";
返回此
echo nl2br($string);
Here is some text
<br /><br />
<pre>
<xml>
<item></item>
<name></name>
</xml>
</pre>
<br /><br />
That includes new lines and carriage returns<br />
what can i do to add <p> and <br> tags but not to the text below inside<br />
the <pre> tags </pre>.<br />
<br />
<pre class="brush: csharp; title: ;" title="">
<br>
public MainPage()
{
// select the culture (de, it, fr, tr are supported)
var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc
// this controls the UI strings
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
// this controls number and date formatting (optional in this sample)
//System.Threading.Thread.CurrentThread.CurrentCulture = ci;
// initialize the component<br>
InitializeComponent();
}
</pre>
似乎忽略了有两个<br><br>
标签。我认为这可能与评论有关。
使用此代码。
function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<pre class="brush: csharp; title: ;"\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre class="brush: csharp; title: ;">'.$b.'</pre>','<pre class="brush: csharp; title: ;">'.str_replace("<br />", "\n", $b)."</pre>", $string);
}
}
}
return $string;
}
echo my_nl2br($description);
但是它忽略了<pre class="brush: csharp; title: ;" title="">
部分中的回车。
输出此
public MainPage()
{
// select the culture (de, it, fr, tr are supported)
var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc
// this controls the UI strings
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
// this controls number and date formatting (optional in this sample)
//System.Threading.Thread.CurrentThread.CurrentCulture = ci;
// initialize the component
InitializeComponent();
}
答案 0 :(得分:4)
PHP手册评论的解决方案:
function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string);
}
}
}
return $string;
}
答案 1 :(得分:0)
这是旧的,但也许其他人仍然在研究这个,所以......
在您的示例中,您使用
<pre class="brush: csharp; title: ;" title="">
但是在您的代码中匹配
<pre class="brush: csharp; title: ;">
答案 2 :(得分:0)
只需将此代码段添加到您的页面中
$(document).ready(function(){
var str_arr = document.getElementsByTagName("pre");
for(var i = 0; i < str_arr.length; i++){
str_arr[i].innerHTML = str_arr[i].innerHTML.replace(/<br>/g, "");
}
});
这将从<br>
中删除<pre>