Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t get preg_match()无法从其他网站获取内容_Php_Regex_Web_Preg Match - Fatal编程技术网

Php Can';t get preg_match()无法从其他网站获取内容

Php Can';t get preg_match()无法从其他网站获取内容,php,regex,web,preg-match,Php,Regex,Web,Preg Match,我试图从一个外部网站获取一个值,该网站为标记和preg_match()提供了一个正则表达式,但它不起作用 我的代码 $file = file_get_contents('http://www.investing.com/indices/us-spx-500'); $regexp = '/\<span class\=\"arial_26 inlineblock pid-166-last\" id\=\"last_last\" dir\=\"ltr\"\>(.*?)\<\/spa

我试图从一个外部网站获取一个值,该网站为标记和
preg_match()
提供了一个正则表达式,但它不起作用

我的代码

$file = file_get_contents('http://www.investing.com/indices/us-spx-500');

$regexp = '/\<span class\=\"arial_26 inlineblock pid-166-last\" id\=\"last_last\" dir\=\"ltr\"\>(.*?)\<\/span>/';
preg_match($regexp, $file, $string1);

print_r(array_values($string1));
如果你从网站上查看源代码,它应该是特定的标签


提前感谢。

只需删除
,不要在单引号字符串中使用转义

$text = '<span class="arial_26 inlineblock pid-166-last" id="last_last" dir="ltr">1,880.02</span>';
$regex = '{<span class="arial_26 inlineblock pid-166-last" id="last_last" dir="ltr">(.*)</span>}';
preg_match($regex, $text, $matches);
echo $matches[1].PHP_EOL;
$text='1880.02';
$regex='{(.*)}';
preg_match($regex,$text,$matches);
echo$matches[1].PHP_EOL;

只需在双引号字符串中转义(模式字符除外),这种情况下的
完全没有影响。您的常规分组模式
(.*)
表示“零个或多个字符,或零”

编辑:
的无关性在上述特定字符串中是真实的,但在更广泛的上下文中不是真实的(即
1880.02


检索所需文本的最正确的方法是,实际上,

只需删除
,而不使用单引号字符串中的转义

$text = '<span class="arial_26 inlineblock pid-166-last" id="last_last" dir="ltr">1,880.02</span>';
$regex = '{<span class="arial_26 inlineblock pid-166-last" id="last_last" dir="ltr">(.*)</span>}';
preg_match($regex, $text, $matches);
echo $matches[1].PHP_EOL;
$text='1880.02';
$regex='{(.*)}';
preg_match($regex,$text,$matches);
echo$matches[1].PHP_EOL;

只需在双引号字符串中转义(模式字符除外),这种情况下的
完全没有影响。您的常规分组模式
(.*)
表示“零个或多个字符,或零”

编辑:
的无关性在上述特定字符串中是真实的,但在更广泛的上下文中不是真实的(即
1880.02


检索所需文本的最正确的方法是,实际上,它不起作用,因为如果不向Investment.com传递用户代理,Investment.com不会返回任何内容。以下代码正常工作:

$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
  )
);
$context = stream_context_create($options);
$file = file_get_contents('http://www.investing.com/indices/us-spx-500',false,$context);
$regexp = '/\<span class=\"arial_26 inlineblock pid-166-last\" id=\"last_last\" dir\=\"ltr\"\>(.*?)<\/span>/';
preg_match($regexp, $file, $string1);
print_r(array_values($string1));
$options=array(
“http'=>数组(
'方法'=>“获取”,
'header'=>“接受语言:en\r\n”。
“用户代理:Mozilla/5.0(iPad;U;CPU OS 3_2,如Mac OS X;en-us)AppleWebKit/531.21.10(KHTML,如Gecko)版本/4.0.4 Mobile/7B33B Safari/531.21.102011-10-16 20:23:10\r\n”//i
)
);
$context=stream\u context\u create($options);
$file=文件获取内容('http://www.investing.com/indices/us-spx-500“,假,$上下文);
$regexp='/\(.*?/”;
预匹配($regexp,$file,$string1);
打印(数组值($string1));

另外,您只需要在该字符串中转义/,无需转义=

它不起作用,因为如果您不向Investment.com传递用户代理,Investment.com不会返回任何内容。以下代码正常工作:

$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad 
  )
);
$context = stream_context_create($options);
$file = file_get_contents('http://www.investing.com/indices/us-spx-500',false,$context);
$regexp = '/\<span class=\"arial_26 inlineblock pid-166-last\" id=\"last_last\" dir\=\"ltr\"\>(.*?)<\/span>/';
preg_match($regexp, $file, $string1);
print_r(array_values($string1));
$options=array(
“http'=>数组(
'方法'=>“获取”,
'header'=>“接受语言:en\r\n”。
“用户代理:Mozilla/5.0(iPad;U;CPU OS 3_2,如Mac OS X;en-us)AppleWebKit/531.21.10(KHTML,如Gecko)版本/4.0.4 Mobile/7B33B Safari/531.21.102011-10-16 20:23:10\r\n”//i
)
);
$context=stream\u context\u create($options);
$file=文件获取内容('http://www.investing.com/indices/us-spx-500“,假,$上下文);
$regexp='/\(.*?/”;
预匹配($regexp,$file,$string1);
打印(数组值($string1));

另外,您只需要在该字符串中转义/,无需转义=

PHP具有解析HTML的内置工具,正则表达式在这里尤其不合适,因为您正在寻找具有id属性的节点

// you set the user_agent with the name you want
$opts = [ 'http' => [ 'user_agent' => 'obliglobalgu' ] ];
// to create a stream context 
$context = stream_context_create($opts);
// set the stream context for DOMDocument::loadHTMLFile 
libxml_set_streams_context($context); 

$url = 'http://www.investing.com/indices/us-spx-500';

libxml_use_internal_errors(true); // avoid eventual libxml errors to be displayed

$dom = new DOMDocument;
$dom->loadHTMLFile($url);

$spanNode = $dom->getElementById('last_last');

if ($spanNode)
    echo $spanNode->nodeValue;

libxml_clear_errors();

PHP有内置的工具来解析HTML,正则表达式在这里特别不合适,因为您正在寻找具有id属性的节点

// you set the user_agent with the name you want
$opts = [ 'http' => [ 'user_agent' => 'obliglobalgu' ] ];
// to create a stream context 
$context = stream_context_create($opts);
// set the stream context for DOMDocument::loadHTMLFile 
libxml_set_streams_context($context); 

$url = 'http://www.investing.com/indices/us-spx-500';

libxml_use_internal_errors(true); // avoid eventual libxml errors to be displayed

$dom = new DOMDocument;
$dom->loadHTMLFile($url);

$spanNode = $dom->getElementById('last_last');

if ($spanNode)
    echo $spanNode->nodeValue;

libxml_clear_errors();

您不需要转义=它正在处理TXT文件,而我转义=符号。只是尝试而没有转义,没有任何进展,但感谢提示。您不需要转义=它正在处理TXT文件,而我转义=符号。只是尝试而没有逃避,没有进展,但感谢提示。值1880.02将每秒更改一次。这是一个指标,所以当市场开放时,它会像第二个指标一样发生变化。那么这个代码会起作用吗?我想从外部网站获得价值。@Treps是的,我知道。这是一个你的文本的例子。显然,相关部分是正则表达式。您可以轻松地用文本更改
$text
!值1880.02将每秒更改一次。这是一个指标,所以当市场开放时,它会像第二个指标一样发生变化。那么这个代码会起作用吗?我想从外部网站获得价值。@Treps是的,我知道。这是一个你的文本的例子。显然,相关部分是正则表达式。您可以轻松地用文本更改
$text
!天哪,它在工作。现在我学到了一些新东西。非常感谢,朋友该死的,它起作用了。现在我学到了一些新东西。非常感谢,朋友DAh,这也是一个非常有效和不错的解决方案。谢谢,伙计。我想这个方法比较慢,因为它必须解析DOM树,而不仅仅是搜索文本中的字符串。这也是一个非常有效和不错的解决方案。谢谢你,伙计。我认为这个方法比较慢,因为它必须解析DOM树,而不仅仅是在文本中搜索字符串