查找字符串中出现的字符串并替换-php
我目前正在开发我自己制作的标题系统。因此,我尝试使用查找字符串中出现的字符串并替换-php,php,string,string-operations,Php,String,String Operations,我目前正在开发我自己制作的标题系统。因此,我尝试使用#u 1#-#u 4#对文本进行编号,然后在标题中进行转换。因此,您将标题的文本写在数字和第二个#之间。例如: #_1Testheadline# #_2second headline under the first one# #_2another headline under the first one# #_1second headline (not under the first one)# 将成为 1. Testheadline 1.1
#u 1#
-#u 4#
对文本进行编号,然后在标题中进行转换。因此,您将标题的文本写在数字和第二个#
之间。例如:
#_1Testheadline#
#_2second headline under the first one#
#_2another headline under the first one#
#_1second headline (not under the first one)#
将成为
1. Testheadline
1.1 second headline under the first one
1.2 another headline under the first one
2. second headline (not under the first one)
希望你明白我的意思
因此,第一个示例(其中包含#u 1#
)是在WYSIWYG编辑器中编写的,这是正常的html代码,然后存储在数据库中的变量中
我现在想要的是,当我从数据库中获取这个变量时,我想用真实标题替换所有的伪标题(查看),所以第一个示例将成为第二个
因此,我需要找到所有出现的#u 1
,替换为
,继续直到#
并替换为
。标题的第二、第三和第四“层”也是如此。如果我能做到这一点,我仍然不会完成,因为现在,标题前面需要数字,因此我可以简单地使用4个计数器:
var firstlayercount = 0,
secondlayercount = 0;
var thirdlayercount = 0,
fourthlayercount = 0;
根据找到的标题,将它们增加并设置回0,所以实际上这应该是比较容易的部分。但更难的问题是,如何在字符串中找到
#u 1
,替换它,然后转到#
并替换它?并多次执行此操作?使用preg\u replace\u回调函数
$str = '#_1Testheadline#
#_2second headline under the first one
#_2another headline under the first one
#_1second headline (not under the first one)';
// Current level
$level = 0;
echo preg_replace_callback('/#?\s*#_(\d)+/',
function($x) use (&$level) {
// Close previous tag if it is
$out = ($level ? "</h{$level}>\n" : '');
// Save current level and open tag
$level = $x[1];
return $out . "<h{$level}>";
},
// Don't forget to close the last tag
$str) . "<h{$level}>";
$str=”##1Testheadline#
#_第一个标题下的第二个标题
#_2第一个标题下的另一个标题
#_1第二个标题(不在第一个标题下);
//当前水平
$level=0;
回显preg_replace_回调('/#?\s*.#(\d)+/',
功能($x)使用(&$level){
//关闭上一个标记(如果是)
$out=($level?\n):“”);
//保存当前级别并打开标记
$level=$x[1];
退回$out。”;
},
//别忘了关闭最后一个标签
$str)。"";
结果
<h1>Testheadline#</h1>
<h2>second headline under the first one</h2>
<h2>another headline under the first one</h2>
<h1>second headline (not under the first one)<h1>
<ol>
<li>Testheadline#
<ol>
<li>second headline under the first one</li>
<li>another headline under the first one</li>
</ol>
<li>second headline (not under the first one)</li>
</ol>
Testheadline#
第一个标题下的第二个标题
第一个标题下的另一个标题
第二个标题(不在第一个标题下)
更新1 您可以使用相同的方法构建有序列表
$level = 0;
echo preg_replace_callback('/#?\s*#_(\d)+/',
function($x) use (&$level) {
if($x[1] > $level) $out = "\n<ol>\n";
else if ($x[1] < $level) $out = "</li>\n</ol>\n";
else $out = "</li>\n";
$level = $x[1];
return $out . "<li>";
},
$str) . "</li>\n</ol>\n";
$level=0;
回显preg_replace_回调('/#?\s*.#(\d)+/',
函数($x)使用(&$level){
如果($x[1]>$level)$out=“\n\n”;
如果($x[1]<$level)$out=“\n\n”;
else$out=“\n”;
$level=$x[1];
返回$out。“”;
},
$str)。“ \n\n”;
结果
<h1>Testheadline#</h1>
<h2>second headline under the first one</h2>
<h2>another headline under the first one</h2>
<h1>second headline (not under the first one)<h1>
<ol>
<li>Testheadline#
<ol>
<li>second headline under the first one</li>
<li>another headline under the first one</li>
</ol>
<li>second headline (not under the first one)</li>
</ol>
测试标题#
第一个标题下的第二个标题
第一个标题下的另一个标题
第二个标题(不在第一个标题下)
更新2 css样式列表
ol{
列表样式:无;
计数器复位:li;
}
李:以前{
反增量:李;
内容:柜台(li,“.”);
}
测试标题#
第一个标题下的第二个标题
第一个标题下的另一个标题
第二个标题(不在第一个标题下)
这是PHP还是JS?您已经为PHP添加了标记,但代码是JSPHP的,javascript会更容易,但需要是PHP本身。我认为这段代码不必关闭它生成的其他标记。因此,开始级别始终为零。感谢这段代码。总的来说,它似乎是有效的,但出于某种原因,所有标题仍然包括第二个(结束)
,这不应该是这样的。你知道为什么吗?而且,老实说,我不完全理解你的代码中发生了什么,我该如何做相反的事情,同时增加数字,这样它就变成了数字,而不仅仅是标题(就像我上面的例子一样)?啊,现在我有问题了,你的代码中没有结尾#
(我首先也忘了),这就是为什么……当然,标题不会一个接一个,这就是为什么我也会使用结尾的标题,所以它是#u 1Test#这里是一些普通文本,没有标题#u 2Second heading#
等等,如果你知道我关于你的更新的意思,很抱歉把你搞糊涂了,“正常”html有序列表对我来说不起作用,因为我需要像我上面的例子那样,不仅是缩进,还有1、1.1、1.2、1.2.1、1.2.2、2
等等,这在html有序列表中是不可能的,这基本上就是hole项目的内容。。