2025-03-02 02:37:32
修改后截取和手环出错:
if($tmpcontents)
{
$wordsdic=explode("/n/r",$tmpcontents); //这行出错
$i=0;
$ttwords=array();
foreach($wordsdic as $value) //这行出错
/n/r拆分不了,我以为只是发帖手误,建议拆分后的$wordsdic输出观察一下。
问题是无法得到$arr=array('陈一','刘二','李四','张三');
只有$arr=array('陈一 刘二 李四 张三');
这是file_get_contents获得的吗,仔细检查,看看姓名之间的分隔符是什么,没有/n/r,explode是分不开的。根据你的最后补充,我看应该使用空格分隔来拆分,但如果是从文件获得,分隔符是换行,不是空格。换行的表示与文件格式相关,UNIX是\n,WINDOWS是\r\n,MAC是\r,是不是很复杂,为什么不用file
用file()出错
if($tmpcontents)
{
$wordsdic=explode("/n/r",$tmpcontents); //这行出错
$i=0;
$ttwords=array();
foreach($wordsdic as $value) //这行出错
你知道你写的/n/r是什么意思吗?我前面说了半天你都没看呀,转义符的方向反了,应该是\n这样写,而且具体是\n还是\r与文件的格式有关。这是第一个需要搞清楚的问题。
出错是什么意思?根据错误提示来修改语句,看了错误提示没有,找到错误的地方没有
现在用file()读出来的内容print_r后是Array ( [0] => 啊啊啊 [1] => 阿埃二氏病变 [2] => 阿埃二氏手术 ......)
if($tmpcontents)
{
$wordsdic=explode("\n",$tmpcontents); //Warning: explode() expects parameter 2 to be string
将 $wordsdic=explode("\n",$tmpcontents)改成$wordsdic=explode("\n",$tmpcontents[1])后出错消失,但 数据不对。
我是小白,请多指教,(抱拳)
file的结果就可以使用呀?有什么问题,为什么不用file
explode报错是参数2不是字符串,你把这个变量值是什么,为什么要带如这个变量去作为字符串拆分
再说使用file直接得到数组,不要file_get_contents+explode方法自己做数组,除非自己明白再做什么
function __construct($dic_file='b.txt')
$tmpcontents=$this->getFileContents($dic_file);
if($tmpcontents)
{
$this->wordsdic=$tmpcontent;
......
function isWords($words)
{
echo '是否存在本词:'.array_search($words,$this->wordsdic).'';
......
getFileContents方法是如何实现的呢,需要自己分析,或者调试一些返回结果$tmpcontents
的类型,我没有看见源码、没有进行调试,无法判断$tmpcontents可以进行哪些下一步的使用。
$tmpcontent形如
Array ( [0] => 啊啊啊 [1] => 阿埃二氏病变 [2] => 阿埃二氏手术
可它没法传到array_search($words,$this->wordsdic)
这样可以吗?array_search($words,$tmpcontent)
一样不行。因为它们属于两个function
function __construct($dic_file='b.txt'){}
function isWords($words){}
不知道楼主有没有想过怎样才得行没有,是不是应该把$tmpcontent存入对象的属性里面呢:
$this->wordsdic = $tmpcontent;