PHP正则表达式被解析了2次
我正在编写一个PHP脚本,它接受纯文本文件任务列表并解析如下内容:PHP正则表达式被解析了2次,php,regex,Php,Regex,我正在编写一个PHP脚本,它接受纯文本文件任务列表并解析如下内容: 项目名称标题,因为它以分号结尾:==项目 任务以-任务名称开始 @标记名用@tagname @使用@done 还有更多 下面的PHP函数是替换的内容 function get_marked_up_todo($todo){ $todo = htmlspecialchars($todo,ENT_QUOTES)."\n\n"; $search = array('/(.+:)(.+)\n\n/sU', //
- 项目名称标题,因为它以分号结尾:==项目
- 任务以
-任务名称开始
- @标记名用
@tagname
- @使用
@done
- 还有更多
function get_marked_up_todo($todo){
$todo = htmlspecialchars($todo,ENT_QUOTES)."\n\n";
$search = array('/(.+:)(.+)\n\n/sU', // Get projects
'/(- ([^\@\n]+).+)/', // Get todos
'/(.+:)/', // Get headings
'/\n([^\<\n].+)/', // Get notes
'/- (.+@done)/', // Get done
'/(@due\([^\)]+\))/', // Get due tags
'/(@(?!due)[^\s]+)/', // Get tags
"/\t/",
'/`(.*?)`/', // inline code
);
$replace = array("<div class=\"project\">\n$1$2\n</div>\n\n",
'<span class="todo"><input type="checkbox" value="'.trim('\2').'"> \1</span>',
'<h1>\1</h1>',
"\n\t<span class=\"note\">$1</span>",
'<span class="bullet-done">? ? ??</span> - <strike>\1</strike>',
'<span class="tag due">\1</span>',
'<span class="tag">\1</span>',
"\t<span class=\"tab\"></span>",
'<code>\1</code>',
);
return preg_replace($search, $replace, $todo);
}
在上面的搜索和替换数组中,这两个数组中的最后一项是我添加的一个新模式,用于查找用反标记包装的内联代码,如Markdown内联代码 问题是,在输出上,每个任务项行都会在任务行的前面添加一个复选框输入字段,并且在该复选框的值中,我的代码正在被解析 只有当我添加一个类似于内联代码的项目或我添加的任何其他项目(例如用于粗体文本和斜体的正则表达式)时,所有其他替换正则表达式才会显示在复选框值中 为什么我的出现在值为HTML的复选框中,而其他复选框都没有出现 我已经设置了一个演示来显示PHP的输出-
下面是完整的代码
<?php
echo "<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>TODO.todo</title></head>
<body>
<style>
.project {
line-height: 4px;
}
.bullet-done {
font-weight: bold;
font-style: normal;
color: rgba(0,114,62,1.0);
}
.note{
display: block;
color: rgba(133,130,102,1.0);
font-weight: normal;
font-style: normal;
}
.todo {
display: inline-block;
}
.tag {
font-weight: bold;
font-style: normal;
color: rgba(160,46,43,0.6);
}
body {
background: rgba(239,233,183,1.0);
color: rgba(0,0,0,0.31);
font-weight: normal;
font-style: normal;
}
h1 {
font-weight: bold;
font-style: normal;
background: rgba(0,0,0,0.06);
color: rgba(188,100,74,1.0);
width: 100%;
line-height: 34px;
}
.tab{
display: inline-block;
width:0px;
height: 0px;
background: #000000;
</style><pre>";
$todo = 'Version 1:
This file is in TaskPaper format.
Tabs are used to indent.
Each task begins with a "- ".
Projects end with a ":".
Tags are in the format "@tag_name".
All other lines (such as these) are considered as notes,
and are to be ignored.
- User signup
- Register for an account
- Log in @done
- Forget password
- Manage users
- Create users @in_progress
- Delete users
- User profile page @40%
- Blog
- Creating new posts @done
- Comments @done
- Moderating comments @done
This is my todo list:
This is a note about the list.
- this is an item @done
- and this is @me
this is a note about my done item
- this is the last @java @shopping @item @done
This is a second list:
- Add more funk to something @work @java
- Send something somewhere @work @email @due(12 Aug 07)
- this is an example
- dfgdfg
ggg
hfghf
- hgh
- dfygdfgdf
List:
- gdfgdf `inline code` hhf
- gdfgdf
- dfgdfg @done
';
echo get_marked_up_todo($todo);
echo '</pre></body></html>';
function get_marked_up_todo($todo){
$todo = htmlspecialchars($todo,ENT_QUOTES)."\n\n";
$search = array('/(.+:)(.+)\n\n/sU', // Get projects
'/(- ([^\@\n]+).+)/', // Get todos
'/(.+:)/', // Get headings
'/\n([^\<\n].+)/', // Get notes
'/- (.+@done)/', // Get done
'/(@due\([^\)]+\))/', // Get due tags
'/(@(?!due)[^\s]+)/', // Get tags
"/\t/",
'/`(.*?)`/', // inline code
);
$replace = array("<div class=\"project\">\n$1$2\n</div>\n\n",
'<span class="todo"><input type="checkbox" value="'.trim('\2').'"> \1</span>',
'<h1>\1</h1>',
"\n\t<span class=\"note\">$1</span>",
'<span class="bullet-done">? ? ??</span> - <strike>\1</strike>',
'<span class="tag due">\1</span>',
'<span class="tag">\1</span>',
"\t<span class=\"tab\"></span>",
'<code>\1</code>',
);
return preg_replace($search, $replace, $todo);
}
如果我正确理解了这个问题,您的问题是当您使用自己的
替换时,
标记如下所示:
但您希望它不包括..
部分,如下所示:
如果我的理解是正确的,那么您只需修复负责呈现
标记的RegExp,即:
“/([^\@\n]+).+)/,//获取待办事项
它的工作方式是从-
获取所有内容,直到出现@
或换行符(\n
)。您要将DoIt添加到背景勾选中:
“/([^\@\n]+).+)/”,//获取待办事项
这将使RegExp在遇到第一个`时停止捕获,并将修复您的问题(同样,如果我理解正确)。非常不清楚。您应该在显示当前输出的同时显式添加预期输出。谢谢,我现在更了解问题的原因。理想情况下,我希望为内联代码、粗体文本、斜体文本等添加正则表达式。这些新类型都围绕一个文本,并且有一个结束标记,当前正则表达式除了这个新的代码标记外,没有任何带结束标记的模式。有了这些包装模式,他们也会在找到模式后想要文本,所以听起来我应该做第二个todos
Regex来将原始文本存储在其中,并将其用作输入值。感谢您的帮助或者可以修改todo
regex,使其包含所有这些文本样式模式的模式,并将其全部包含在todo的1个模式中,并将其捕获到不同的捕获组。我将修改todo regex以包含所有这些模式。当你回到它比有两个不同的正则表达式更清楚。如果我的回答解决了您的问题,请标记为,以便我们都可以继续:)