simple html dom - space in class name

2019-02-26 02:48发布

I'm using PHP Simple HTML DOM to get element from a source code of a site (not mine) and when I find a ul class that is called "board List",this is not found.I think it might be a problem of space but I don't know how to solve it.

this is a piece of php code:

$html = str_get_html($result['content']); //get the html of the site
$board = $html->find('.board List');  //  Find all element which class=board List,but in my case it doesn't work,with other class name it works

and this is a piece of html code of the site:

<!-- OTHER HTML CODE BEFORE THIS --><ul class="board List"><li id="c111131" class="skin_tbl">
<table class="mback" cellpadding="0" cellspacing="0" onclick="toggleCat('c111131')"><tr>
<td class="mback_left"><div class="plus"></div><td class="mback_center"><h2 class="mtitle">presentiamoci</h2><td class="mback_right"><span id="img_c111131"></span></table>
<div class="mainbg">
<div class="title top"><div class="aa"></div><div class="bb">Forum</div><div class="yy">Statistiche</div><div class="zz">Ultimo Messaggio</div></div>
<ul class="big_list"><!-- OTHER HTML AFTER THIS -->

4条回答
聊天终结者
2楼-- · 2019-02-26 02:55

The answer is that: You cannot use spaces in classnames. spaces are the seperaters of classes

if you have <div class="container wrapper-something anothersomething"></div> then you can use .container, .wrapper-something or .anothersomething as a selector and you allways match that div.

So in your code you have <ul class="board List">, so to get a match in a css-selector ($html->find('{here_comes_the_css_selector}');) you can use eather .board or .List as the selctor

Therefor your line $board = $html->find('.board List'); should look more like this:

$board = $html->find('.board.List');
// maches every element who has class 'board' AND 'List'
// Here it is really important that there is no spaces between those 2 selectors

// or

$board = $html->find('.List');
// maches every element who has class 'List'

// or

$board = $html->find('.board');
// maches every element who has class 'board'
查看更多
我命由我不由天
3楼-- · 2019-02-26 03:00
$board = $html->find('[class="board List"]');

With this syntax SimpleHTMLDOM finds elements with multiple class attribute

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-26 03:11

I solved it by removing board from the find parameter,as this:

$board = $html->find('.List');

now the parser seems to work correctly

查看更多
The star\"
5楼-- · 2019-02-26 03:14

With simple you would probably want to use:

$html->find('*[class="board List"]', 0);

If you really want to use:

$html->find('.board.List', 0);

Then use this one.

查看更多
登录 后发表回答