Wordpress PHP post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alinn
Forum Newbie
Posts: 2
Joined: Tue Dec 18, 2012 7:52 am

Wordpress PHP post

Post by alinn »

How can I post on wordpress with this script and how can I edit the post if images from the original page have been changed?

Code: Select all

<?php // File: MatchAllDivMain.php
 
// Read html file to be processed into $data variable
$data = file_get_contents('test.html');
 
// Commented regex to extract contents from <div class="main">contents</div>
//  where "contents" may contain nested <div>s.
//  Regex uses PCRE's recursive (?1) sub expression syntax to recurs group 1
$pattern_long = '{           # recursive regex to capture contents of "main" DIV
<div\s+class="main"\s*>              # match the "main" class DIV opening tag
  (                                   # capture "main" DIV contents into $1
    (?:                               # non-cap group for nesting * quantifier
      (?: (?!<div[^>]*>|</div>). )++  # possessively match all non-DIV tag chars
    |                                 # or 
      <div[^>]*>(?1)</div>            # recursively match nested <div>xyz</div>
    )*                                # loop however deep as necessary
  )                                   # end group 1 capture
</div>                               # match the "main" class DIV closing tag
}six';  // single-line (dot matches all), ignore case and free spacing modes ON
 
// short version of same regex
$pattern_short = '{<div\s+class="main"\s*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(?1)</div>)*)</div>}si';
 
$matchcount = preg_match_all($pattern_long, $data, $matches);
// $matchcount = preg_match_all($pattern_short, $data, $matches);
echo("<pre>\n");
if ($matchcount > 0) {
    echo("$matchcount matches found.\n");
//  print_r($matches);
    for($i = 0; $i < $matchcount; $i++) {
        echo("\nMatch #" . ($i + 1) . ":\n");
        echo($matches[1][$i]); // print 1st capture group for match number i
    }
} else {
    echo('No matches');
}
echo("\n</pre>");
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Wordpress PHP post

Post by Eric! »

I don't know what you're trying to do, but if you want Wordpress to process php inside your posts, you have to have a plug-in installed for that like Exec-PHP.

If this is some kind of tool that you want to run on whatever page is being viewed, then you need to modify the template.

I see you got this code from here http://www.devnetwork.net/viewtopic.php ... 70#p551340. It makes me think you're trying to use this to scrape another website. If that's the case it is up to you where you put it in word press depending on how you want to display the information.

If you want to "edit the post when the original images have been changed" then I'm guessing you'll want to make a local copy of the original images and point to them instead of the links used in the text you scraped.
alinn
Forum Newbie
Posts: 2
Joined: Tue Dec 18, 2012 7:52 am

Re: Wordpress PHP post

Post by alinn »

What I need to modify in this code to get content from to 2 or more divs id or classes?
Post Reply