PHP字符串到Int转换丢失值(使用simple_html_dom) [英] PHP String to Int Conversion Losing Value (using simple_html_dom)

查看:162
本文介绍了PHP字符串到Int转换丢失值(使用simple_html_dom)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用simple_html_dom来解析具有特定类的元素的页面。我成功检索了这些元素但似乎无法将它们转换为可用变量(即整数,因此我可以执行'if'语句)。



看来它们是某种形式的对象,我到处搜索了好几个小时,但没有运气。似乎没有太多支持simple_html_dom。



这是我的代码:

  /////// //////// 
$ html = new simple_html_dom();

//加载文件
$ html-> load_file($ getURL);
$ getName = $ html - > find('。ddlabel',0);
$ getSeats = $ html-> find('。dddefault',3);
///////////////

$ scraped_name =$ getName;
$ scraped_seats =$ getSeats;

$ seats = intval($ scraped_seats);

echo $ scraped_name。 <峰; br> 中。 $ scraped_seats。 <峰; br><峰; br> 中;
echo gettype($ seat)。 <峰; br> 中。 $座位。 <峰; br> 中;

//如果座位是可用的,如果($ seat!== 0){...}
p $ p>

有些回声只是出于我的目的而试图解决这个问题。另外我可能会在foreach()循环中添加所有这些代码,不确定是否重要。我正在读一个附加到url的csv文件,这就是foreach()循环的用途。但我不认为这会弄乱我的问题。



感谢您的帮助!

解决方案

根据文档您必须提取内容:

  $ scraped_seats =(int)$ getSeats-> plaintext; 


so I'm using simple_html_dom to parse a page for elements with a particular class. I successfully retrieve those elements but cannot seem to get them converted into usable variables (ie an integer so I can do an 'if' statement).

It seems they are an object of some kind, and I've searched everywhere for hours, but no luck. There doesn't seem to be much support for simple_html_dom.

Here's my code:

///////////////
$html = new simple_html_dom();  

// Load a file 
$html->load_file($getURL); 
$getName = $html -> find('.ddlabel', 0);
$getSeats = $html->find('.dddefault', 3); 
///////////////

$scraped_name = "$getName";
$scraped_seats = "$getSeats";

$seats = intval($scraped_seats);

echo $scraped_name . "<br>" . $scraped_seats . "<br><br>";
echo gettype($seats) . "<br>" . $seats . "<br>";

// If seats are avavilable, send a message
if ($seats !== 0) { ... }

Some of the echos are just for purposes of me trying to figure this out. Also I might add that all this code in in a foreach() loop, not sure if that matters. I'm reading a csv file of things to append to a url and that's what the foreach() loop is for. But I don't think that's messing with the problem I have.

Thanks for any help!

解决方案

According to the documentation you would have to extract the content:

$scraped_seats = (int)$getSeats->plaintext;

这篇关于PHP字符串到Int转换丢失值(使用simple_html_dom)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆