2017-02-19 106 views
0

我有相同的PHP preg_match脚本,检查相同的文件,在两台Linux服务器上,他们不会导致相同的方式(相同的PHP版本)。试着检查我的当地赛道上是否有马匹。我试过preg_last_error显示没有错误。preg_match正在一台服务器上工作,但没有其他

$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

我想这是由于php'的'版本。前段时间我有类似的问题。 – math2001

+0

可能与您的实际问题无关,但考虑使用解析器而不是试图在DOM上摆弄正则表达式。 – Jan

+0

[相同的文件,但不是相同的内容。](https://i.stack.imgur.com/p9Z67.png) – revo

回答

1

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

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

如果它不是为行尾,那么你实际上并没有寻找一个常规:你做到这一点使用\s -

你可以通过匹配任何空白,而不是确切的空白解决这个问题表达式,只是一个字符串。所以我会说绝对不使用的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

+0

是的工作。我忘了\ s。这绝对是Linux和Windows之间的区别。当它保存在我的Windows PC上时,它使用的是与Linux机器不同的新行。以为我失去了理智。谢谢! – Bill

相关问题