Heres the scenario. Currently I'm trying to write a script that will
1) allow me to take a .html page and open it as an array.
2) implode the array so I can use preg_match_all
3) Have preg_match_all return an array of all 4 elements (each it's own array)
4) Sort through all elements in the returned array (in position 2) so I can alter the div tag it find.
5) Perform a str_replace on each div tag.
6) Then, when I have created the new div tags, go through the source again and replace all the old div tags I updated with the new ones.
7) Save the source.
I am fine up until step six(6). Thats where I'm having troubles; updating the .html file with the new div tags (without removing the comments seperating them...
Here is the source I've written so far:
<?php
/*
// xcms.parser.php
// This file contains the scripts to parse a document
// and extract all editable regions in the file.
// May need to following expression...
// if(preg_match_all('/(<div .+?>.+?<\/div>)/ms', $divs, $oldDivs)) {
*/
class Parser {
function feed($file) {
// -------------------
// SHOW FILE FOR PARSING
echo "* Parsing file: ".$file."<br />";
// -------------------
// DEFINE VARIABLES
$divArray = array();
$oldDivArray = array();
// -------------------
// FEED FILE TO PARSER
if(!($source = @file($file))) {
die("Failed to open file $file");
}
// -------------------
// FIND XCMS EDIT TAGS, EXTRACT DIVS AND ALTER THEM TO CONTENTEDITABLE="TRUE".
/*
// NOTE: preg_match_all returns $matches - an array in 4 parts [0], [1], [2], [3]
// [0] = All parts of the preg
// [1] = Just the start comments
// [2] = Just the divs [this is what we want.]
// [3] = Just the end comments
*/
$source = implode('', $source);
if(preg_match_all('/(<!-- start xcms edit -->)(.+?)(<!-- end xcms edit -->)/ms', $source, $matches)) {
echo "* Found ".count($matches[2])." matches of xcms editable regions.<br />";
foreach($matches[2] as $div){
trim($div);
if(!($editableDivs = @str_replace('<div', '<div contenteditable="true"', $div))) {
die("Failed to update divs!");
} else {
echo "* Made div editable.<br/>";
trim($editableDivs);
array_push($divArray, $editableDivs);
}
}
} else {
die("Failed to match anything");
}
}
}
?>
Any help on this subject would be greatly appreciated.
Yours,
Goodson