This may be off topic a bit.
I am trying to add data to tables in the database by copying the data from another app and pasting it into an online form in a MODx application. The problem I am having is with the regular expression I am trying to use to parse the data. The following code demo illustrates the problem.
When I use this tool
http://www.spaweditor.com/scripts/regex/, I get exactly what I want. When I try what I think to be the same in my MODx snippit, I don’t get the same results. I hope that one of you MODx gurus will also know enough PHP and regex to help me out.
The regex:
/(Verse|Chorus|Misc) (\d+)\s((?s:.*?))(?=\s{3,})/
I would like to rewrite this using the embedded comments format but am not there yet so here is a parsing of my regex.
/
(Verse|Chorus|Misc) (\d+) // capture the lyrics type (verse, chorus, etc.) followed by a number
\s // the newline
( // start capture of the lyrics text
(?s:.*?) // set "." to any token and find many of them ungreedily
) // stop the capture of the lyrics text...
(?=\s{3,}) // which is followed by (look ahead) three or more white space characters
/
The subject:
Mary Had A Little Lamb
Verse 1
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow
Verse 2
Everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go
Verse 3
It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.
Verse 4
It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school
Copyright 18xx
The result from the tool (this looks great--I could use this to insert into the tables):
Array
(
[0] => Array
(
[0] => Verse 1
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow
[1] => Verse 2
Everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go
[2] => Verse 3
It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.
[3] => Verse 4
It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school
)
[1] => Array
(
[0] => Verse
[1] => Verse
[2] => Verse
[3] => Verse
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[3] => Array
(
[0] =>
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow
[1] =>
Everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go
[2] =>
It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.
[3] =>
It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school
)
)
My snippit code:
<?php
$reTitle = "/\A.*/";
$reCCLISongNumber = "/CCLI Song No. ([0-9]*)/";
$reCopyAuthor = "/© (.*)\s(.*)/";
$reLyrics = "/(Verse|Chorus|Misc) (\d+)\s((?s:.*))(?=\s{2,})/";
$song = <<< END
Mary Had A Little Lamb
Verse 1
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow
Verse 2
Everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go
Verse 3
It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.
Verse 4
It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school
END;
//-----------------------------------------------------------------------------------
preg_match($reTitle, $song, $title);
echo "<p>$title[0]</p>\n";
preg_match($reCCLISongNumber, $song, $songnum);
echo "<p>$songnum[1]</p>\n";
preg_match($reCopyAuthor, $song, $copyauth);
echo "<p>$copyauth[1]</p>\n";
echo "<p>$copyauth[2]</p>\n";
$result = preg_match_all($reLyrics, $song, $lyrics, PREG_SET_ORDER);
echo "\n\nMatch successful: $result\n\n";
echo "\n\n\n\n";
echo "<p>\n";
foreach ($lyrics as $verse) {
foreach ($verse as $field) {
echo "$field<br />\n";
}
}
echo "</p>\n";
return;
?>
The result to the page request in MODx (I’m using a simple template):
...
...
</ul>
</div>
<p>Mary Had A Little Lamb</p>
<p></p>
<p></p>
<p></p>
Match successful: 0
<p>
</p>
</body>
</html>