At first take a look to my script:
首先来看看我的脚本:
<?php
$list=file_get_contents('txt.txt');
$name=explode('\r\n',$list); //file exploding by \n because list is one under one
foreach($name as $i1){
echo ($i1);
echo '</br>';
}
?>
it is showing my result (according to list) :
它显示我的结果(根据列表):
morgan daniel martin sopie tommy
But I have used </br>
so that it must be showing :
但是我已经使用了 以便它必须显示:
morgan
daniel
martin
sopie
tommy
But maybe I am missing something.
但也许我错过了一些东西。
3 个解决方案
#1
2
In this situation better to use preg_replace
... as you don't understand it well, can use below tricks... see the below code...
在这种情况下更好地使用preg_replace ...因为你不太了解它,可以使用下面的技巧...见下面的代码...
<?php
$list=file_get_contents('txt.txt');
echo implode('<br>',explode(' ',$list));
?>
HTML:
morgan<br>daniel<br>martin<br>sopie<br>tommy
Output Preview:
morgan
daniel
martin
sopie
tommy
if you want a break at the end too... use below one...
如果你想在最后休息一下......使用下面一个......
echo implode('<br>',explode(' ',$list)).'<br>';
#2
1
Use php file
instead to loop through lines of a doc:
使用php文件来循环遍历doc的行:
#3
1
explode()
splits as per the given boundary string so you have to include all the whitepace charactes.
explode()按照给定的边界字符串进行拆分,因此您必须包含所有的空白字符。
You can find the execution see online
您可以在线查看执行情况
So better go for regex which can be used with preg_split() with regex pattern "\s" - the whitespace character class
所以最好去正则表达式,它可以与preg_split()一起使用正则表达式模式“\ s” - 空白字符类
---edited---
//$list = $list = preg_replace('<br>',' ',$list);; // replace the <br> with space
$list = str_replace('<br>',' ',$list); //better than preg_replace as regex dont
// wok better for html tags
---EOF edited---
$name = preg_split('/\s+/',$list);
echo '<pre>';
print_r($name);
echo '</pre>';
-----------o/p---------
Array ( [0] => morgan [1] => daniel [2] => martin [3] => sopie [4] => tommy )
note:
The replace since it's a hardcoded string function.
替换,因为它是一个硬编码的字符串函数。
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
正则表达式需要更长的时间,因为它需要解析正则表达式字符串(即使你设置了RegexOptions.Compiled)然后在正则表达式字符串中执行它然后制定结果字符串。但是如果你真的想确定,在一百万次迭代器中执行每次迭代并对结果计时。
Go through this:
通过这个:
Shorthand Character Classes(\s)
速记字符类(\ s)
#1
2
In this situation better to use preg_replace
... as you don't understand it well, can use below tricks... see the below code...
在这种情况下更好地使用preg_replace ...因为你不太了解它,可以使用下面的技巧...见下面的代码...
<?php
$list=file_get_contents('txt.txt');
echo implode('<br>',explode(' ',$list));
?>
HTML:
morgan<br>daniel<br>martin<br>sopie<br>tommy
Output Preview:
morgan
daniel
martin
sopie
tommy
if you want a break at the end too... use below one...
如果你想在最后休息一下......使用下面一个......
echo implode('<br>',explode(' ',$list)).'<br>';
#2
1
Use php file
instead to loop through lines of a doc:
使用php文件来循环遍历doc的行:
#3
1
explode()
splits as per the given boundary string so you have to include all the whitepace charactes.
explode()按照给定的边界字符串进行拆分,因此您必须包含所有的空白字符。
You can find the execution see online
您可以在线查看执行情况
So better go for regex which can be used with preg_split() with regex pattern "\s" - the whitespace character class
所以最好去正则表达式,它可以与preg_split()一起使用正则表达式模式“\ s” - 空白字符类
---edited---
//$list = $list = preg_replace('<br>',' ',$list);; // replace the <br> with space
$list = str_replace('<br>',' ',$list); //better than preg_replace as regex dont
// wok better for html tags
---EOF edited---
$name = preg_split('/\s+/',$list);
echo '<pre>';
print_r($name);
echo '</pre>';
-----------o/p---------
Array ( [0] => morgan [1] => daniel [2] => martin [3] => sopie [4] => tommy )
note:
The replace since it's a hardcoded string function.
替换,因为它是一个硬编码的字符串函数。
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
正则表达式需要更长的时间,因为它需要解析正则表达式字符串(即使你设置了RegexOptions.Compiled)然后在正则表达式字符串中执行它然后制定结果字符串。但是如果你真的想确定,在一百万次迭代器中执行每次迭代并对结果计时。
Go through this:
通过这个:
Shorthand Character Classes(\s)
速记字符类(\ s)