0

I have an article with text and multiple images in it and need to get just images and just text, separately.

Now I have this code and it just returns last image in article:

preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $article, $img);

How to select all images and do reverse for getting just text?

Thank you

Jakob
  • 3,493
  • 4
  • 26
  • 42
  • Don't use regex for this, use a DOM parser. http://stackoverflow.com/a/1732454/362536 – Brad Apr 30 '14 at 22:46
  • possible duplicate of [PHP preg\_match to find multiple occurrences](http://stackoverflow.com/questions/2029976/php-preg-match-to-find-multiple-occurrences) – Anonymous Apr 30 '14 at 22:46
  • python+beautifulsoup? soup.find_all('img')..['src']? soup.text? I can provide more detail if you can give me a sample data and I can write some POC code – B.Mr.W. Apr 30 '14 at 22:59

2 Answers2

1
$text = preg_replace('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', '', $article);
preg_match_all('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $article, $images);

//use $images and $text
Sean Johnson
  • 5,567
  • 2
  • 17
  • 22
  • 1
    In HTML parsing, you almost never want to use a greedy match `.+`. Use an ungreedy one `.+?` – HamZa Apr 30 '14 at 22:53
1

You can use the DOM for that:

$imgSrc = array();
$txt = '';

$dom = new DOMDocument();
@$dom->loadHTML($article);

$imgs = $dom->getElementsByTagName('img');

foreach ($imgs as $img) {
    $imgSrc[] = $img->getAttribute('src');
}

$xpath = new DOMXPath($dom);
$textNodes = $xpath->query('//*[not(self::script) and not(self::style)]/text()');
foreach ($textNodes as $textNode) {
    $tmp = trim($textNode->textContent);
    $txt .= (empty($tmp)) ? '' : $tmp . PHP_EOL;

}

XPath query details:

// means anywhere in the DOM tree
* means all tag nodes
[.....] defines a condition
not(self::script) : the current node must not be a script node
text() returns the text node

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125