简体   繁体   English

正则表达式不适用于PHP + cURL

[英]Regex not working with PHP + cURL

I'm making a cURL request to take data from one page. 我正在发出cURL请求以从一页获取数据。
The returned HTML content is (view image below): 返回的HTML内容是(下面的查看图):

http://i.imgur.com/Sbhepbw.png http://i.imgur.com/Sbhepbw.png

I want to filter the following content 'Saldo principal: R$ 27,50'. 我要过滤以下内容“ Saldo委托人:R $ 27,50”。
I'm trying to do this with regular expression but the script is returning an empty array. 我正在尝试使用正则表达式执行此操作,但是脚本返回的是空数组。

This is my code: 这是我的代码:

$content = getContent();
preg_match_all("/Saldo Principal: R\$ [0-9]{1,},[0-9]{1,}/i", $content, $result);
echo '<pre>';
print_r($result);

This is the result: 结果如下:

Array ( [0] => Array ( ) )

I tested this using a regular expression site and everything is ok. 我使用正则表达式站点进行了测试,一切正常。 Why is not working with PHP? 为什么不使用PHP?

http://i.imgur.com/dcV2VnC.png http://i.imgur.com/dcV2VnC.png

Make use of DOMDocument Class. 利用DOMDocument类。

$content = getContent();
$dom = new DOMDocument;
@$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('p') as $tag) {
       $pcontent[]=$tag->nodeValue;
}
echo trim($pcontent[1]); //"prints" Saldo principal: R$ 27,50

or you could put it in a single line. 或者您可以将其放在一行中。 Like this. 像这样。

echo $dom->getElementsByTagName('p')->item(1)->nodeValue;

Inside a double quoted string the backslash is treated as an escape character. 在双引号字符串内,反斜杠被视为转义字符。 thus: 从而:

either you single quote: 您单引号:

preg_match_all('/Saldo Principal: R\\$ [0-9]{1,},[0-9]{1,}/i', $content, $result);

or double escape: 或双逃逸:

preg_match_all("/Saldo Principal: R\\\\$ [0-9]{1,},[0-9]{1,}/i", $content, $result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM