2017-09-04 76 views
-2

我每次運行我的文件時都會測試一些行,但它們並不總是相同的行。我想要做的就是在另一條線上測試它們是否相同。這個問題的部分原因是因爲在每行結尾處都有數字,我不想在進行比較時考慮這些數字。如何忽略行尾的數字以比較字符串?

在片段下面我想補充一點,對,我知道會出來,這樣,如果它發現它,它返回一個查詢,我需要運行到屏幕上的行每行比較其他功能

error: there were errors found during runtime 12.0 
warning: incorrect info 1.0 
warning: file at 'C://localhost/myfiles' not found 0.0 
warning: file at 'C://localhost/css' not found 0.0 
warning: file at 'C://localhost/js' not found 0.0 
warning: file at 'C://localhost/assets' not found 5615612.0 

這是我試圖做的,但我不知道如何忽略數字,以便實際上可以比較線條。 所有這些發生在第一部分運行之後,我得到的數字大於0.0

意思是這條線得到比較。

error: there were errors found during runtime 12.0 
warning: incorrect info 1.0 
warning: file at 'C://localhost/assets' not found 5615612.0 

if ("error: there were errors found during runtime 12.0" == line){ 
    //run this query for database runtimeDB 
} 
if("warning: incorrect info 1.0" == line){ 
    //this query 
    //+1 for incorrectInfoDB 
} 
if("warning: file at 'C://localhost/myfiles' not found 0.0" == line){ 
    //this query 
    //+1 for incorrectInfoDB 
} 
if("warning: file at 'C://localhost/css' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/css' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/js' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 
if("warning: file at 'C://localhost/assets' not found 0.0" == line){ 
    //this query 
    //+1 for outerfilesDB 
} 

這是我試過的實際代碼。有重用的代碼可以被優化。最後,所有內容都保存在一個數組中,並且所有數組都會根據查詢查詢的數據庫打印出來。

function compare_RT(){ 
    var text = $('textarea').val(); 
    var lines = text.split("\n"); 
    var runtimeDB= []; 
    lines.forEach(function(line){ 

     var string0 = "error: there were errors found during runtime" 

     if(line.includes(string0)){ 
      runtimeDB.push(line); 
     } 
    }); 
return runtimeDB; 
} 
function compare_IInfo(){ 
    var text = $('textarea').val(); 
    var lines = text.split("\n"); 
    var incorrectInfoDB= []; 
    lines.forEach(function(line){ 

     var string0 = "error: there were errors found during runtime" 
     var string1 = "warning: file at 'C://localhost/myfiles' not found" 

     if(line.includes(string0)){ 
      incorrectInfoDB.push(line); 
     } 
     if(line.includes(string1)){ 
      incorrectInfoDB.push(line); 
     } 
    }); 
return incorrectInfoDB; 
} 

function getLines() { 
 
    var text = $('textarea').val(); 
 
    var lines = text.split("\n"); 
 
    var requiredLines = []; 
 
    lines.forEach(function(line) { 
 
    number = line.match(/\d+\.\d+$/); 
 
    if (number != null && number[0] > 0) 
 
     requiredLines.push(line); 
 
    }); 
 
    return requiredLines; 
 
} 
 

 

 
$(document).ready(
 
    function() { 
 
    $('#printlogs').html(getLines().join('<br>')); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea style="width:100%;height:120px"> 
 
error: there were errors found during runtime 12.0 
 
warning: incorrect info 1.0 
 
warning: file at 'C://localhost/myfiles' not found 0.0 
 
warning: file at 'C://localhost/css' not found 0.0 
 
warning: file at 'C://localhost/js' not found 0.0 
 
warning: file at 'C://localhost/assets' not found 5615612.0 
 
</textarea> 
 

 
<h4>Error Log</h4> 
 
<printlogs id="printlogs"></printlogs>

+0

所以,如果我理解正確的,你必須檢查是如果消息是相同的,而忽略行號。正確? – Rajesh

+0

是的,這是正確的 – learningbyexample

+0

我不明白錯誤在哪裏,你已經使用'.include()'來檢查,你也可以使用'.contains()'。 –

回答

0

我敢肯定,你可以將它適應您的解決方案。

function trim_numbers($string) { 
    //Get the numbers after the last whitespace. 
    //strrchr — Find the last occurrence of a character in a string. 
    $numbers = strrchr($string, ' '); 

    //Replace found numbers with nothing (''). 
    //$numbers is now equal to ' 12.0'. 
    return str_replace($numbers, '', $string); 
} 

echo trim_numbers("error: there were errors found during runtime 12.0"); 

輸出:

error: there were errors found during runtime

Working snippet

相關問題