我正在构建一个票务系统,但我不想放置其中一条消息
*************************** 在此行上方回复 *************** ********
Gmail 倾向于用他们的“引用文本”做一个很好的主意。有谁知道可以轻松执行此操作的任何预制脚本或方法?我正在尝试将他们的回复通过管道传回我们的系统。
谢谢, 克里
最佳答案
我想你需要像我的全数组差异函数这样的东西:
/**
Full Array Diff implemented in pure php, written from scratch.
Copyright (C) 2011 Andres Morales <yo@kijote.com.ar>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
http://www.gnu.org/licenses/gpl.html
About:
I needed a function to compare a email and its response but array_diff()
does not cover my expectations. So I reimplement a full array diff function.
You can use it directly in your code and adopt to your needs.
Contact:
yo@kijote.com.ar <Andres Morales>
**/
function farray_diff($array1, $array2){
$out = array();
$max_arr = count($array1) > count($array2)? count($array1) : count($array2);
$i = 0;
$j = 0;
while($i < $max_arr && $j< $max_arr){
if($array1[$i] == $array2[$j]){
array_push($out, $array1[$i]);
}
else {
if(in_array($array1[$i], array_slice($array2, $j))){
for($k = $j; $k<$max_arr; $k++){
if($array1[$i]==$array2[$k]){
array_push($out, $array2[$k]);
$j = $k;
break;
}
else{
array_push($out, array('o' => '', 'n' => $array2[$k]));
}
}
}
elseif(in_array($array2[$j], array_slice($array1, $i))){
for($k = $i; $k<$max_arr; $k++){
if($array2[$j]==$array1[$k]){
array_push($out, $array1[$k]);
$i = $k;
break;
}
else {
array_push($out, array('o' => $array1[$k], 'n' => ''));
}
}
}
else{
if(!empty($array1[$i]))
array_push($out, array('o' => $array1[$i], 'n' => $array2[$j]));
else
array_push($out, array('o' => '', 'n' => $array2[$j]));
}
}
$i++; $j++;
}
return $out;
}
因此,您可以像下面的示例一样简单地使用它:
$str1 = "This is a simple text that can you reply, so can you do it?";
$str2 = "I response in your text: This is a simple text (no so simple) that can be replied, so can you do it? Yes, I can!";
// Printing the full array diff of single space exploded strings
print_r(farray_diff(explode(' ', $str1), explode(' ', $str2)));
返回:
Array
(
[0] => Array
(
[o] =>
[n] => I
)
[1] => Array
(
[o] =>
[n] => response
)
[2] => Array
(
[o] =>
[n] => in
)
[3] => Array
(
[o] =>
[n] => your
)
[4] => Array
(
[o] =>
[n] => text:
)
[5] => This
[6] => is
[7] => a
[8] => simple
[9] => text
[10] => Array
(
[o] =>
[n] => (no
)
[11] => Array
(
[o] =>
[n] => so
)
[12] => Array
(
[o] =>
[n] => simple)
)
[13] => that
[14] => can
[15] => Array
(
[o] =>
[n] => be
)
[16] => Array
(
[o] =>
[n] => replied,
)
[17] => Array
(
[o] =>
[n] => so
)
[18] => Array
(
[o] =>
[n] => can
)
[19] => you
[20] => Array
(
[o] => reply,
[n] =>
)
[21] => Array
(
[o] => so
[n] =>
)
[22] => Array
(
[o] => can
[n] =>
)
[23] => Array
(
[o] => you
[n] =>
)
[24] => do
[25] => it?
[26] => Array
(
[o] =>
[n] => Yes,
)
[27] => Array
(
[o] =>
[n] => I
)
[28] => Array
(
[o] =>
[n] => can!
)
这就像一个简单的差异,但没有“+”和“-”,在简单解析后,两者都被替换为“o”(旧)和“n”(新)数组键。您可以使用以下函数来解析结果:
function format_response($diff_arr){
$new = false;
echo '<span class="old">';
foreach($diff_arr as $item)
{
$content = '';
if (!is_array($item)){
$new = false;
$content = $item;
}
else
if (empty($item['o']) && !empty($item['n'])){
$new = true;
$content = $item['n'];
}
if($old_new != $new){
if($new)
echo '</span><span class="new">';
else
echo '</span><span class="old">';
}
echo $content . (!empty($content)?' ':'');
$old_new = $new;
}
echo '</span>';
}
因此,除了使用简单的“print_r”之外,您还可以使用以下方法解析数组:
format_response(farray_diff(explode(' ', $str1), explode(' ', $str2)));
你得到(按照这个例子)是这样的:
<span class="old"></span><span class="new">I response in your text: </span><span class="old">This is a simple text </span><span class="new">(no so simple) </span><span class="old">that can </span><span class="new">be replied, so can </span><span class="old">you do it? </span><span class="new">Yes, I can! </span>
显然,要正确显示结果,您需要定义 css“旧”和"new"类,但有一些不同,pex:不同的前景色:
<style>.old{color: #808080;}.new{color:#000000}</style>
对于 html 电子邮件,或者您可以修改 format_response 函数以显示非 html 电子邮件。
注意:如您所见,我的功能是免费软件,并且受 GNU 通用公共(public)许可证的约束。
希望对你有帮助。
关于PHP - 类似 Gmail 的电子邮件内容分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846265/
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption
Ruby是否有逐步调试器,类似于Perl的“perl-d”? 最佳答案 ruby-debug(对于ruby1.8),debugger(对于ruby1.9),byebug(对于ruby2.0)以及trepanning系列都有一个-x或--trace选项。在调试器内部,命令setlinetrace将打开或关闭线路跟踪。这是themanualforruby-debug原来的答案已经修改,因为数据噪声文章的链接,唉,不再有效了。还添加了ruby-debug的后继者 关于ruby-Ruby
假设您在Ruby中执行此操作:ar=[1,2]x,y=ar然后,x==1和y==2。是否有一种方法可以在我自己的类中定义,从而产生相同的效果?例如rb=AllYourCode.newx,y=rb到目前为止,对于这样的赋值,我所能做的就是使x==rb和y=nil。Python有这样一个特性:>>>classFoo:...def__iter__(self):...returniter([1,2])...>>>x,y=Foo()>>>x1>>>y2 最佳答案 是的。定义#to_ary。这将使您的对象被视为要分配的数组。irb>o=Obje
我使用Jekyll运行博客,并认为我会解决RedcarpetMarkdown解释器,因为它是developedandusedbyGitHub.好吧,我只是碰巧遇到了一个错误,去检查问题,然后foundthis.Maintainersays,"Asyouprobablyhavenoticed(harharharhar)Idon'thavetimetomaintainRedcarpetanymore.It'snotapriorityforme(IfindMarkdownthoroughlyboring)andit'snotapriorityforGitHub,becausewenolong
我正在尝试提取方括号内的内容。到目前为止,我一直在使用它,它有效,但我想知道我是否可以直接在正则表达式中使用某些东西,而不是使用这个删除功能。a="Thisissuchagreatday[coolawesome]"a[/\[.*?\]/].delete('[]')#=>"coolawesome" 最佳答案 差不多。a="Thisissuchagreatday[coolawesome]"a[/\[(.*?)\]/,1]#=>"coolawesome"a[/(?"coolawesome"第一个依赖于提取组而不是完全匹配;第二个利用前瞻和
关于如何使用git设置类似Dropbox的服务,您有什么建议吗?您认为git是解决此问题的合适工具吗?我在考虑使用git+rush解决方案,你觉得怎么样? 最佳答案 检查这个开源项目:https://github.com/hbons/SparkleShare来自项目的自述文件:Howdoesitwork?SparkleSharecreatesaspecialfolderonyourcomputer.Youcanaddremotelyhostedfolders(or"projects")tothisfolder.Theseprojec
使用Ruby1.8.6/Rails2.3.2我注意到在我的任何ActiveRecord模型类上调用的任何方法都返回nil而不是NoMethodError。除了烦人之外,这还破坏了动态查找器(find_by_name、find_by_id等),因为即使存在记录,它们也总是返回nil。不从ActiveRecord::Base派生的标准类不受影响。有没有办法追踪在ActiveRecord::Base之前拦截method_missing的是什么?更新:切换到1.8.7后,我发现(感谢@MichaelKohl)will_paginate插件首先处理method_missing。但是will_pa