Php 解析一些没有编号的文本
我的文本文件包含以下内容,例如:Php 解析一些没有编号的文本,php,Php,我的文本文件包含以下内容,例如: 1. I code 2. I eat 我只想把句子解析成一些字符串,数一数字符串中的每个单词,忽略数字。但结果我想再写一次数字。所以我想得到的结果是: 1. I(1) code(4) 2. I(1) eat(3) 我想。首先,我必须用“”替换数字 主程序: $file = "C:/AppServ/www/kbbi/cobaangka.txt"; $lines = file($file); foreach($lines as $line_num =>
1. I code
2. I eat
我只想把句子解析成一些字符串,数一数字符串中的每个单词,忽略数字。但结果我想再写一次数字。所以我想得到的结果是:
1. I(1) code(4)
2. I(1) eat(3)
我想。首先,我必须用“”替换数字
主程序:
$file = "C:/AppServ/www/kbbi/cobaangka.txt";
$lines = file($file);
foreach($lines as $line_num => $line) {
$first = checkFirstWord2($line);
$count = count(explode('',$first);
echo $first.'('.$count.')'; // but how to write again the number 1. and 2. ?
}
我不知道;I don’我不知道该怎么写句子的编号:1.
和2.
?
请帮帮我,谢谢:)
测试如果每个句子都在新行上,类似这样的测试应该有效
$lines = explode("\n", $string);
$der = preg_replace('/\d\. /', '', $lines);
现在,print\r($der)代码>返回:
Array
(
[0] => I code
[1] => I eat
)
解释
首先,我们将句子分解成新行:
explode("\n", $string);
这为我们提供了一个如下所示的数组:
Array
(
[0] => 1. I code
[1] => 2. I eat
)
现在,你想从句子中删除行号(我按“这是什么?”)。为此,我们需要使用正则表达式:/\d\/代码>
preg_replace('/\d\. /', '', $lines);
你的想法是对的,但是你的一些功能是错误的
首先将生产线分成几个部分
存储行号以备以后使用
检查剩下的每个单词并打印出单词的长度
-
注意-如果知道行号总是按顺序排列,则不需要单独存储行号。如果它们总是按顺序排列,您可以使用foreach循环中可用的索引。尝试以下方法:
- 首先,当然,获取文件
- 然后,第二步,循环数据,在循环下,分解每一行,然后在分解的行上迭代,得到每个单词的长度
- 当然,最后,把它们重新组合起来
样本值:
1. I code
2. I eat
3. I sleep
4. You jump
5. They curse all
6. I saw
考虑这个例子:
$final = '';
$file = "cobaangka.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line) {
$pieces = array_filter(explode(' ', $line)); // explode them
$line_num = array_shift($pieces); // get the line number
// each words, calculate the number of characters
$complete = array_map(function($var){
return $var.'('.strlen($var).') ';
}, $pieces);
// and finally, put them back together
$final .= $line_num .' '.implode($complete) . "\n";
}
echo '<pre>';
print_r($final);
什么是3
和4
?字长?@thg435是每个单词中的字母数
$file = "file.txt";
$lines = file($file);
foreach($lines as $line_num => $line) {
$line_parts = explode(' ', $line); //split the line into parts
$line_number = array_shift($line_parts);//take out the first part of the line, the number
echo $line_number . ' ';//add space so it doesn't get stcuck to other letters
//loop through each of the line parts, and count the number of letters
foreach ($line_parts as $part) {
if (empty($part)) continue; //ignore spaces if any
$part = trim($part);//some cleanup for trailing spaces
$lettercount = strlen($part);
echo "$part($lettercount) "; //print the output word by word
}
echo "<br>";
}
1. I(1) code(4)
2. I(1) eat(3)
1. I code
2. I eat
3. I sleep
4. You jump
5. They curse all
6. I saw
$final = '';
$file = "cobaangka.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line) {
$pieces = array_filter(explode(' ', $line)); // explode them
$line_num = array_shift($pieces); // get the line number
// each words, calculate the number of characters
$complete = array_map(function($var){
return $var.'('.strlen($var).') ';
}, $pieces);
// and finally, put them back together
$final .= $line_num .' '.implode($complete) . "\n";
}
echo '<pre>';
print_r($final);
1. I(1) code(4)
2. I(1) eat(3)
3. I(1) sleep(5)
4. You(3) jump(4)
5. They(4) curse(5) all(3)
6. I(1) saw(3)