Home Parsing content only storing one result
Reply: 1

Parsing content only storing one result

The Local Peon
1#
The Local Peon Published in 2018-01-13 07:54:52Z

I am having some issues getting my extremely messy code to submit data properly. Currently I am scraping a website which harbors many images and trying to collect them all and store them accordingly via my WordPress the_content selection.

Here's what I've got going so far, this is returning the images almost without any issues when I load it via a standard loop.

foreach ($html2->find('.entry-content img') as $image) {
    $imageurl  = $image->src;
    $new = '<img src="' . $imageurl . '" style="height: auto; width: 100%;margin-bottom: 3px;">';
    print  $thecontent = htmlspecialchars($new); print '<br>';
} foreach ($html2->find('iframe') as $video) {
    $videourl = $video->src;;
    $new = '<iframe src="' . $videourl . '" scrolling="no" frameborder="0" width="100%" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>';
    print  $thecontent = htmlspecialchars($new); print '<br>';
}

The above code will return something that looks like this containing 1 - how ever many images + videos we're trying to gather.

<img src="https://www.example.com/some-image-path-here.jpg" style="height: auto; width: 100%;margin-bottom: 3px;">
<img src="https://www.example.com/some-image-path-here.jpg" style="height: auto; width: 100%;margin-bottom: 3px;">
<img src="https://www.example.com/some-image-path-here.jpg" style="height: auto; width: 100%;margin-bottom: 3px;">
<img src="https://www.example.com/some-image-path-here.jpg" style="height: auto; width: 100%;margin-bottom: 3px;">

Now here's what I am using to try and upload the content to my WordPress site (everything except $content appears to be working properly.

$content = $thecontent;
$my_post = array(
    'post_title'    => wp_strip_all_tags( trim( $title ) ),
    'post_content'  => $content,
    'post_status'   => 'publish',
    'post_author'   => 2,
    'post_category' => array(2),
    'post_date' => date('Y-m-d H:i:s')
);
$post_id = wp_insert_post( $my_post );
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');

The above code returns the following within my WordPress the_content portion, which is the first image only, how can I make this work?

<img src="https://www.example.com/some-image-path-here.jpg" style="height: auto; width: 100%;margin-bottom: 3px;">

Note: The portion which is storing the WordPress data is being loaded inside of our initial parse loop, but outside of the loop which is collecting the images + videos.

Nigel Ren
2#
Nigel Ren Reply to 2018-01-13 08:04:02Z

Each time round the loop your just collecting one piece of information and setting $thecontent to that field and printing it out. You need to add these to together to get a string containing all of the content...

$thecontent = '';
foreach ($html2->find('.entry-content img') as $image) {
    $imageurl  = $image->src;
    $new = '<img src="' . $imageurl . '" style="height: auto; width: 100%;margin-bottom: 3px;">';
    $thecontent .= htmlspecialchars($new).'<br>';
} 
foreach ($html2->find('iframe') as $video) {
    $videourl = $video->src;;
    $new = '<iframe src="' . $videourl . '" scrolling="no" frameborder="0" width="100%" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>';
    $thecontent .= htmlspecialchars($new).'<br>';
}
print $thecontent;

Note where in each loop I use .= to append the new content to the end of the list. The final content is printed out and should be used

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306845 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO