在PHP中将标记列表转换为html列表
我今天一直在为我的一个项目做减价转换器。我迷路了,只是浏览了一下代码,注意到我有大量的列表转换。下面是我的代码,我刚刚提供了处理列表的代码,我真的可以提供一些关于缩短代码的建议,我只是看不到我写的东西,需要一双新的眼睛在PHP中将标记列表转换为html列表,php,regex,markdown,Php,Regex,Markdown,我今天一直在为我的一个项目做减价转换器。我迷路了,只是浏览了一下代码,注意到我有大量的列表转换。下面是我的代码,我刚刚提供了处理列表的代码,我真的可以提供一些关于缩短代码的建议,我只是看不到我写的东西,需要一双新的眼睛 <?php class Markdown { private static $html = ''; private static $list_types = array( array( '>>', '<ul>
<?php
class Markdown {
private static $html = '';
private static $list_types = array(
array(
'>>',
'<ul>',
'</ul>'
),
array(
'>>>',
'<ol>',
'</ol>'
)
);
private static $list_patterns = array(
'/-[ ]+(.+)/' => ' >>\1',
'/[0-9]{1}\. (.+)/' => ' >>>\1'
);
public static function convertText($markdown = array()) {
$markdown = explode("\n", strip_tags($markdown));
foreach ($markdown as &$line) {
$line = htmlentities($line, ENT_QUOTES, 'UTF-8');
foreach (self::$list_patterns as $pattern => $replace) {
if (!is_array($line) && preg_match($pattern, $line)) {
$para = false;
$line = preg_replace($pattern, $replace, $line);
$type = 0;
foreach (self::$list_types as $key => $val) {
if (preg_match('/ ' . $val[0] . ' /', $line))
$type = $key;
}
$line = preg_split('/' . self::$list_types[$type][0] . '/', $line);
$line = array('depth' => strlen($line[0]), 'string' => $line[1], 'type' => $type);
}
}
}
while (!empty($markdown)) {
$snippet = array_shift($markdown);
if (is_array($snippet))
self::makeList($snippet, $markdown);
else
self::$html .= $snippet;
}
return self::$html;
}
private static function makeList($snippet, &$markdown, $last_depth = 0, $close_tag = '') {
if ($last_depth == $snippet['depth'])
self::$html .= sprintf('</li><li>%s', $snippet['string']);
elseif ($last_depth < $snippet['depth'])
self::$html .= sprintf('%s<li>%s', self::$list_types[$snippet['type']][1], $snippet['string']);
elseif ($last_depth > $snippet['depth'])
self::$html .= sprintf('</li>%s<li>%s', $close_tag, $snippet['string']);
$next_snippet = array_shift($markdown);
if (is_array($next_snippet))
self::makeList($next_snippet, $markdown, $snippet['depth'], self::$list_types[$snippet['type']][2]);
else
array_unshift($markdown, $next_snippet);
self::$html .= sprintf('</li>%s', $close_tag);
}
}
?>
基本上,代码会进行大量的模式匹配,对于除list之外的任何模式,它都会在数组“$markdown”中留下一个字符串,对于列表,它会创建一个具有列表类型、深度和字符串的数组。因此,在文本转换结束时,我可以循环通过“$markdown”数组,并通过检查下一项是否为数组来构建嵌套循环结构
很抱歉,如果这个问题看起来很模糊,我只是想得到一些关于如何缩短我的代码的提示,因为我似乎已经写了很多东西
提前谢谢
卢克@他们的主人可能是一个更好的前进道路。但是,我成功地删除了2行代码和未使用的变量$para
<?php
class Markdown {
private static $html = '';
private static $list_types = array(
array('>>','<ul>','</ul>'),
array('>>>','<ol>','</ol>')
);
private static $list_patterns = array(
'/-[ ]+(.+)/' => ' >>\1',
'/[0-9]{1}\. (.+)/' => ' >>>\1'
);
public static function convertText($markdown = array()) {
foreach (explode("\n", strip_tags($markdown)) as &$line) {
$line = htmlentities($line, ENT_QUOTES, 'UTF-8');
foreach (self::$list_patterns as $pattern => $replace) {
if (!is_array($line) && preg_match($pattern, $line)) {
$line = preg_replace($pattern, $replace, $line);
$type = 0;
foreach (self::$list_types as $key => $val) {
if (preg_match('/ ' . $val[0] . ' /', $line))
$type = $key;
}
$line = preg_split('/' . self::$list_types[$type][0] . '/', $line);
$line = array('depth' => strlen($line[0]), 'string' => $line[1], 'type' => $type);
}
}
}
while (!empty($markdown)) {
$snippet = array_shift($markdown);
if (is_array($snippet))
self::makeList($snippet, $markdown);
else
self::$html .= $snippet;
}
return self::$html;
}
private static function makeList($snippet, &$markdown, $last_depth = 0, $close_tag = '') {
if ($last_depth == $snippet['depth'])
self::$html .= sprintf('</li><li>%s', $snippet['string']);
elseif ($last_depth < $snippet['depth'])
self::$html .= sprintf('%s<li>%s', self::$list_types[$snippet['type']][1], $snippet['string']);
elseif ($last_depth > $snippet['depth'])
self::$html .= sprintf('</li>%s<li>%s', $close_tag, $snippet['string']);
$next_snippet = array_shift($markdown);
if (is_array($next_snippet))
self::makeList($next_snippet, $markdown, $snippet['depth'], self::$list_types[$snippet['type']][2]);
else
array_unshift($markdown, $next_snippet);
self::$html .= sprintf('</li>%s', $close_tag);
}
}
正如@Theifmaster所说的,它已经很全面而且非常棒了,所以我最终使用了它,因为我真的不打算给它添加任何更有用的东西。虽然开始编写代码,但有很好的学习经验。您是否意识到已经有了一个完整的PHP降价实现?它是经过许可的BSD/GPL(您喜欢的),因此将其嵌入到您的软件中应该不会有问题。小偷说了什么。除非你真的对轮子感兴趣,否则不要重新发明轮子。我已经看过这个类,在为我删除了所有不必要的东西(我不需要wordpress之类的东西)之后,它仍然有1500行代码。我知道在这1500行中有大量的评论,但我只是希望它可以在一个较小的包中实现。这种轮子确实对我有一些兴趣。谢谢你的评论。@El Yobo实际上,他们使用了一大堆正则表达式。降价是一个非常糟糕的标准(但却是一种非常棒的格式)。同意1500行。您可能很难想出更短的代码。@Luke不要担心1500行代码,要担心构建一个很棒的产品。不要担心:祝你好运!