preg_match正在一台服务器而不是其他服务器

时间:2017-02-19 07:06:00

标签: php regex ftp

我有相同的php preg_match脚本,检查同一个文件,在两个Linux服务器上它们不会产生相同的方式(相同的php版本)。试着检查我当地赛道上是否有马匹。我试过preg_last_error显示没有错误。

$pattern='/<p class=\"clear\" style=\"margin-top:-17px;\">&nbsp;<\/p> -->

    <h4 class=\"lightgreenbg padding\">/';
if (preg_match($pattern, $HTMLcontent)) { echo ("Found races today. <br>"); } else { echo ("No races found."); }

$ HTMLcontent可以找到一个server1server2。不确定这是编码,php还是ftp问题。当我将数据从服务器1 FTP到服务器2时,它也停止在服务器2上工作。但是当我将它下载到我的PC然后FTP服务器2工作正常。很奇怪。

1 个答案:

答案 0 :(得分:1)

如果您的服务器和工作站使用不同的操作系统,这可能是由于行结尾的差异造成的。 Windows / Dos使用\r\n,而linux仅使用\n

您可以通过匹配任何空格而不是确切的空格来解决此问题 - 您可以使用\s执行此操作:

$pattern='/<p class=\"clear\" style=\"margin-top:-17px;\">&nbsp;<\/p> -->\s+<h4 class=\"lightgreenbg padding\">/';

如果它不是行结尾,那么你实际上并没有搜索正则表达式,只是一个字符串。所以我想说绝对不要使用preg_match因为strpos效率更高:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

来自:http://php.net/manual/en/function.strpos.php

相关问题