在字符串中查找字符串的出现并替换 - php

时间:2018-01-29 06:34:47

标签: php string string-operations

我目前正在制作自制的标题系统。因此,我尝试在文本中使用#_1# - #_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)

希望你理解我的意思。

所以第一个例子(其中包含#_1#)是用WYSIWYG-Editor编写的,所以这是普通的html代码,然后存储在数据库的变量中。

我现在想要的是当我从数据库中获取此变量时,我想用真实标题(查看)替换所有伪标题,因此第一个示例将成为第二个。

因此,我需要找到#_1的所有出现,替换为<h1>,继续#并将其替换为</h1>。同样适用于第二,第三和第四层&#34;层&#34;标题如果我能够做到这一点,我仍然无法完成,因为现在,标题需要他们的数字,因此我可以简单地使用4个计数器:

var firstlayercount = 0,
  secondlayercount = 0;
var thirdlayercount = 0,
  fourthlayercount = 0;

并根据找到的标题增加并将它们设置为0,所以实际上这应该是更容易的部分。但更难的问题是,如何找到字符串中的#_1,替换它然后转到#并替换它?这样做几次?

1 个答案:

答案 0 :(得分:2)

使用preg_replace_callback函数

$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}>";

结果

<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>  

更新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";

结果

<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划分样式列表

<STYLE>
ol {
  list-style: none; 
  counter-reset: li; 
}
li:before {
   counter-increment: li; 
   content: counters(li,".") ". "; 
}
</STYLE>
<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>

更新3

通过php进行编号

$cnts = [ 0 ];
echo preg_replace_callback('/#?\s*#_(\d)+/', 
  function($x) use (&$cnts) {
    $level = $x[1]-1;
    if($level < count($cnts)-1) $cnts = array_slice($cnts, 0, $level+1);
    if (! isset($cnts[$level])) $cnts[$level] = 1;
    else $cnts[$level]++;
    $temp = array_slice($cnts, 0, $level+1);
    return "\n" . implode('.', $temp) . ". "; 
  },
  $str) ;

结果

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)