我需要修改许多 SQL 语句以包含递增的数字以跟踪“段落”列中的每个单词。抱歉,我的 PHP 技能有限。阅读文件后,我正在考虑一个 foreach 循环,但无法提出任何实际修改行以包含每个单词的值的方法。
当前 SQL:
INSERT INTO `book` VALUES(1, 1, 1, 'These are the words in a sentence in a chapter in a book.');
INSERT INTO `book` VALUES(1, 1, 2, 'These are the words in the second sentence in a chapter in a book.');
INSERT INTO `book` VALUES(1, 1, 3, 'These are the words in the third sentence in a chapter in a book.');
...and so on.
当前的 VALUES 是“书”、“章”、“段落编号”、“段落”。
目标:
INSERT INTO `book` VALUES(1, 1, 1, 1,'These');
INSERT INTO `book` VALUES(1, 1, 1, 2,'are');
INSERT INTO `book` VALUES(1, 1, 1, 3,'the');
INSERT INTO `book` VALUES(1, 1, 1, 4,'words');
-new- VALUES 是 'book'、'chapter、'paragraph_number'、'paragraph'、'word_number'。
我已经有了书、章和段号。
例如:
INSERT INTO `book` VALUES(1, 5, 16, 'This is a sentence from book 1, chapter 5, paragraph 16.');
将转换为:
INSERT INTO `book` VALUES(1, 5, 16, 1,'This');
INSERT INTO `book` VALUES(1, 5, 16, 2,'is');
INSERT INTO `book` VALUES(1, 5, 16, 3,'a');
INSERT INTO `book` VALUES(1, 5, 16, 4,'sentence');
INSERT INTO `book` VALUES(1, 5, 16, 5,'from');
INSERT INTO `book` VALUES(1, 5, 16, 6,'book');
INSERT INTO `book` VALUES(1, 5, 16, 7,'1,');
INSERT INTO `book` VALUES(1, 5, 16, 8,'chapter');
INSERT INTO `book` VALUES(1, 5, 16, 9,'5,');
INSERT INTO `book` VALUES(1, 5, 16, 10,'paragraph');
INSERT INTO `book` VALUES(1, 5, 16, 11,'16.');
任何帮助将不胜感激......谢谢!
编辑:下面是有效的代码……基于 Akam 的回答……我更改了 SQL 语句。添加了 [SEP] 以将文本与允许我处理文本的其他数据分开。此外,添加了 $x 以增加字数。
SQL:
1, 1, 1,[SEP]These are the words in sentence 1 in chapter 1 in a book.
1, 1, 2,[SEP]These are the words in sentence 2 in chapter 1 in a book.
PHP:
$lines = file('my-book.sql');
$query = "INSERT INTO `book` VALUES \n";
foreach ($lines as $line)
{
$part = preg_split("/\[SEP\]/", $line);
$part[1] = preg_replace('~[\r\n]+~', '', $part[1]);
$words = preg_split("/\s+/", $part[1]);
$x = 1;
foreach($words as $word)
{
$values[] = "(".$part[0]." ".$x.", '".$word."')";
$x++;
}
}
$new_lines = implode(",\n", $values);
$new_lines = $query.$new_lines.";";
echo $new_lines;