php - how to get specific parts in a page using preg_match_all

836

Hello I use preg_match_all function to grab things on a page but when i tried to grab some specific parts like details part it sends me an array !

code structure on that page is

<div class="f slp">DETAILS I WANT TO GET</div>

previously for grab urls n titles I used a code like

//so this gets URLs in href=""
preg_match_all('/a href="([^"]+)" class=l.+?>.+?<\/a>/',$scraped,$results);

but this time I want to grab some details on that page under the structure of

<div class="f slp">DETAILS I WANT TO GET</div>
547

Answer

Solution:

preg_match_all("#<div class=\"f slp\">(.*?)<\/div>#si", $source, $match);

foreach($match[1] as $val) {
    echo $val."<br>";
}
239

Answer

Solution:

Please check out PHP Simple HTML DOM Parser a very easy to use library that makes it pretty easy to extract content from html.

// from the documentation
$html = str_get_html("<div>foo <b>bar</b></div>");
$e = $html->find("div", 0);
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
echo $e->innertext; // Returns: " foo <b>bar</b>"
echo $e->plaintext; // Returns: " foo bar"

read more in manual

People are also looking for solutions to the problem: php - way to check for if sql row exists?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.