Php 文件中的字符串无效
我在处理从文件转换的字符串时遇到了问题,导致该字符串与直接输入时的行为相同: 这是我的test.html文件:Php 文件中的字符串无效,php,string,preg-match,fread,Php,String,Preg Match,Fread,我在处理从文件转换的字符串时遇到了问题,导致该字符串与直接输入时的行为相同: 这是我的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&g
<html>
<font class="editable">
This is editable section 1
</font>
<br><br><hr><br>
<font class="editable">
This is editable section 2
</font>
</html>
这是第1节
这是第2节
这是我的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()时,它在注释掉的字符串上按预期工作,但在从文件创建的字符串上不工作,返回一个空数组
我错过了什么?谢谢。我想您可以简单地使用将文件作为字符串读入变量。
因此: 另外,最好使用绝对路径(如
dirname(\uu文件)“/FILE.ext”
)或前缀为//code>(如”/FILE.ext“
)的相对路径。所以你可以试着换衣服
$file = 'test.html';
进入
甚至
$file = dirname(__FILE__).'/test.html'
问题:
在我看来,正则表达式似乎在处理多行时遇到了问题。这似乎是您传入的字符串(绕过文件\u get\u contents()
)与加载的文件内容之间的差异
解决方案:
更改常规express的值以允许多行:
$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);
$html=为什么不直接使用?这要简单得多。需要注意的是,在您调用return之后,您正在调用fclose($fh)
fclose()
永远不会发生。谢谢,我正在使用在stack或php.net上找到的一段脚本@AlvinWong我已经多次尝试使用file_get_contents来实现这种功能,但始终无法使字符串的行为符合我的预期。你能发布一个有效的例子吗?什么?就像$html=file\u get\u contents($file)
@AlvinWong一样简单,它对我来说永远不起作用。(我承认我是一个php新手)它还返回一个0的数组,当字符串应该返回一个2的数组时,我已经更新了$file部分。然而,放入$html=file\u获取内容($file)代码>在那里仍然不起作用。即使它返回一个字符串,该字符串的行为也与手动输入相同内容的字符串不同。@John使用var\u dump($html)
显示其中的内容。可能的原因是您的文件中有换行符(“\n”
)。我知道它是以字符串的形式出现的,并且似乎可以正常工作。当我尝试将该字符串放入$arrayOfEditables函数时,它的行为与注释掉的相同字符串值的$html版本不同。我尝试过htmlspecialchars和htmlentities之类的方法来让它工作,但都没有成功。returnStartEnd应该在该字符串值上输出一个2的数组。使用readFileToVar和您建议的简单使用file_get_内容,它返回一个0数组,这意味着它的行为不同,而不是我想要的。谢谢JMax和@AlvinWong。更新的表达和工作。更改了文件\u get\u内容的readFileToVar(),它也可以按需要工作。
$expression = '/' . preg_quote($start, '/') . '([\w\s.]*?)'. preg_quote($end, '/') . '/im';
$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);