【问题标题】:String from file not working文件中的字符串不起作用
【发布时间】:2012-11-29 04:57:54
【问题描述】:

我在处理从文件转换的字符串时遇到问题,结果与直接输入该字符串的行为相同:

这是我的 test.html 文件:

<html>

<font class="editable">
This is editable section 1
</font>
<br><br><hr><br>
<font class="editable">
This is editable section 2
</font>

</html>

这是我的 php 文件:

<?php

//RETURN ARRAY OF RESULTS FOUND BETWEEN START & END IN STRING
function returnStartEnd($string,$start,$end){ 
     preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m); 
     $out = array(); 

     foreach($m[1] as $key => $value){ 
       $type = explode('::',$value); 
       if(sizeof($type)>1){ 
          if(!is_array($out[$type[0]])) 
             $out[$type[0]] = array(); 
          $out[$type[0]][] = $type[1]; 
       } else { 
          $out[] = $value; 
       } 
     } 
  return $out; 
};


// RETURN FILE CONTENTS AS A STRING
function readFileToVar($file){
  $fh = fopen($file,'r') or die($php_errormsg);
  $html = fread($fh,filesize($file));
  return $html;
  fclose($fh) or die($php_errormsg);
};

$file = 'test.html';
$html = readFileToVar($file);
// OR
//$html = '<html> <font class="editable"> This is editable section 1 </font><br><br><hr><br><font class="editable"> This is editable section 2 </font> </html>';
$go = 'editable">';
$stop = '<';

$arrayOfEditables = returnStartEnd($html,$go,$stop);
echo "<br>Result:<br>";
var_dump($arrayOfEditables);

?>

注意注释掉的$html。它与 test.html 文件中应该(?)返回的内容相同。尝试运行函数 returnStartEnd() 时,它在注释掉的字符串上按预期工作,但对从文件创建的字符串不起作用,返回一个空数组。

我错过了什么?谢谢。

【问题讨论】:

  • 为什么不直接使用file_get_contents?这简单得多
  • 需要注意的一点是,在您调用 return 之后,您正在调用 fclose($fh)fclose() 永远不会发生。
  • 谢谢,我正在使用我在堆栈或 php.net 上找到的一段脚本。 @AlvinWong 我已经多次尝试使用 file_get_contents 来实现这种功能,但永远无法让字符串表现得像我预期的那样。你能发布一个工作示例吗?
  • 什么?就像$html = file_get_contents($file) 一样简单
  • @AlvinWong 这对我来说永远不起作用。 (我承认我是一个 php 新手)当字符串应该返回一个 2 的数组时,它也返回一个 0 的数组

标签: php string preg-match fread


【解决方案1】:

我认为您可以简单地使用file_get_contents 将文件作为字符串读入变量中。
所以:

$html = file_get_contents($file);

此外,使用绝对路径(如dirname(__FILE__)."/file.ext")或以./ 为前缀的相对路径(如"./file.ext")总是一个好主意。所以你可以尝试改变

$file = 'test.html';

进入

$file = './test.html';

甚至

$file = dirname(__FILE__).'/test.html'

【讨论】:

  • 谢谢,我已经更新了 $file 部分。但是将$html = file_get_contents($file); 放在那里仍然不起作用。即使它返回一个字符串,该字符串的行为也与手动输入相同内容的字符串不同。
  • @John 使用var_dump($html) 显示其中的内容。一个可能的原因是您的文件中有换行符 ("\n")。
  • 我知道它以字符串的形式出现并且似乎可以按预期工作。当我尝试将该字符串放入 $arrayOfEditables 函数时,它的行为与相同字符串值的注释掉的 $html 版本不同。我已经尝试过诸如 htmlspecialchars 和 htmlentities 之类的东西来让它工作,但没有成功。 returnStartEnd 应该在该字符串值上输出一个 2 的数组。使用 readFileToVar 和您简单地使用 file_get_contents 的建议,它返回一个 0 数组,这意味着它的行为不同,而不是我想要的。
【解决方案2】:

问题:

在我看来,正则表达式似乎在处理多行时遇到了问题。这似乎是您传入的字符串(绕过file_get_contents())与加载文件的内容之间的差异。

解决办法:

更改正则表达式的值以允许多行:

$expression = '/' . preg_quote($start, '/') . '([\w\s.]*?)'. preg_quote($end, '/') . '/im';

这个正则表达式寻找起始点,并将起始点和结束点之间的所有值放入一个字符类中。然后,最后,我添加了m 修饰符,将其置于多行模式。

根据我的测试,这两种方式都适合我:

$html = <<<HTML
<html>

<font class="editable">
This is editable section 1
</font>
<br><br><hr><br>
<font class="editable">
This is editable section 2
</font>

</html>
HTML;

$alternate = '<html><font class="editable">This is editable section 1</font><br><br><hr><br><font class="editable">This is editable section 2</font></html>';

var_dump($html);
$expression = '/' . preg_quote('editable">', '/') . '([\w\s.]*?)'. preg_quote('<', '/') . '/im';
var_dump($expression);

preg_match_all($expression, $html, $m);
var_dump($m);

preg_match_all($expression, $alternate, $m);
var_dump($m);

【讨论】:

  • 感谢 JMax 和@AlvinWong。更新了表达并有效。更改了 file_get_contents 的 readFileToVar(),它也可以按预期工作。
猜你喜欢
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 2022-11-04
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 2017-08-17
相关资源
最近更新 更多