ファイル ( php ) を C# のリストに解析して、それを処理できるようにしています。以前に PHP で実行したことがありますが、動作が非常に遅いため、C# の winform アプリケーションを作成しようとしています。
これは、C# で既に持っているコードです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace g7_product_parser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void log(String msg)
{
textBox1.AppendText(msg + "\r\n\r\n");
}
public string[] parseLine(String regel)
{
string[] parsed;
parsed = new string[2];
regel = regel.Replace("array(", "");
regel = regel.Replace("),", "");
regel = regel.Replace(")", "");
string[] temp = regel.Split(',');
foreach (string part in temp)
{
string[] part2 = Regex.Split(part, "=>");
// log(String.Join(",", part2));
if (part2[0].Replace("'", "").Trim() == "keyword_uid")
{
parsed[1] = part2[1].Replace("'", "").Trim();
}
else if (part2[0].Replace("'", "").Trim() == "product_uid")
{
parsed[0] = part2[1].Replace("'", "").Trim();
}
}
return parsed;
}
private void button1_Click(object sender, EventArgs e)
{
String startTime = DateTime.Now.ToString("HH:mm:ss tt");
log("Begin!");
string[] lines = System.IO.File.ReadAllLines("tx_ttproductsv2_keywords_in_products.php");
List<string> entrys = new List<string>();
foreach (string line in lines)
{
// parseLine(line);
log(String.Join(",",parseLine(line)));
Application.DoEvents();
}
String endTime = DateTime.Now.ToString("HH:mm:ss tt");
String totalTime = (DateTime.Parse(endTime) - DateTime.Parse(startTime)).ToString();
log("Klaar!");
log("Start Tijd: " + startTime + "\r\nEind Tijd: " + endTime + "\r\nTotale Tijd: " + totalTime);
}
}
}
解析するファイルは次のようになります。
array('uid' => '1','pid' => '2','keyword_uid' => '1','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '2','pid' => '2','keyword_uid' => '2','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '3','pid' => '2','keyword_uid' => '3','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '4','pid' => '2','keyword_uid' => '4','product_uid' => '1','amount' => '9','in_title' => '0'),
array('uid' => '5','pid' => '2','keyword_uid' => '5','product_uid' => '1','amount' => '18','in_title' => '0'),
array('uid' => '6','pid' => '2','keyword_uid' => '6','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '7','pid' => '2','keyword_uid' => '7','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '8','pid' => '2','keyword_uid' => '8','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '9','pid' => '2','keyword_uid' => '9','product_uid' => '1','amount' => '79','in_title' => '1'),
array('uid' => '10','pid' => '2','keyword_uid' => '10','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '11','pid' => '2','keyword_uid' => '11','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '12','pid' => '2','keyword_uid' => '12','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '13','pid' => '2','keyword_uid' => '13','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '14','pid' => '2','keyword_uid' => '14','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '15','pid' => '2','keyword_uid' => '15','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '16','pid' => '2','keyword_uid' => '16','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '17','pid' => '2','keyword_uid' => '17','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '18','pid' => '2','keyword_uid' => '18','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '19','pid' => '2','keyword_uid' => '19','product_uid' => '1','amount' => '15','in_title' => '1'),
array('uid' => '20','pid' => '2','keyword_uid' => '20','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '21','pid' => '2','keyword_uid' => '21','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '22','pid' => '2','keyword_uid' => '22','product_uid' => '1','amount' => '21','in_title' => '0'),
array('uid' => '23','pid' => '2','keyword_uid' => '23','product_uid' => '1','amount' => '12','in_title' => '0'),
次のようなPHPコードがありました。
require_once 'export/tx_ttproductsv2_keywords_in_products_part.php';
$sql = new Sql;
$tagList = array();
$counter = 0;
$counter2 = 0;
$counter3 = 0;
foreach ($tx_ttproductsv2_keywords_in_products as $key => $value) {
$keywordsList = $sql -> getKeyWords($value['keyword_uid']);
foreach ($keywordsList as $key2 => $value2) {
if(isset($tagList[$value['product_uid']])){
$tagList[$value['product_uid']] .= ", " . $value2['keyword'];
}else{
$tagList[$value['product_uid']] = $value2['keyword'];
}
}
}
このようにして (PHP で)、配列内のエントリが既に存在するかどうかを確認し、それが真の場合は追加するか、新しいエントリを作成します。
私の質問は、C# でそれを行うにはどうすればよいかということです。リストを使用する必要があると思いますが、正確にはわかりません。