I have a newsletter system I am trying to incorporate within a PHP site. The PHP site loads a content area and also loads scripts into the head of the page. This works fine for the code that is generated for the site but now I have the newsletter I am trying to incorporate.
Originally I was going to use an iFrame but the amount of AJAX and jQuery calls makes this quite complex.
So I thought I could use cURL to load the newsletter page as a variable. Then I was going to use RegEx to grab the content between the body tags and place this in the content area. Finally I was going to use RegEx again to search through the head and grab any scripts.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config_live_site."lib/alerts/user/[email protected]"); # URL to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt($ch, CURLOPT_HEADER, 0);
$loaded_result = curl_exec( $ch ); # run!
curl_close($ch);
// Capture the body content and place in $_content
if (preg_match('%<body>([\s\S]*)</body>%', $loaded_result, $regs)) {
$_content .= $regs[1];
} else {
$_content .= "<p>No content to display.</p>";
}
// Capture the scripts and place in the head
if (preg_match('%(<script type="text/javascript">[\s\S]*</script>)%', $loaded_result, $regs)) {
$headDetails .= $regs[0];
}
This works most of the time but if there is a script in the body of the document it captures down to the last /script'.
My question is two-fold I guess...
A. Is there a better overall approach (My deadline is very short so it needs to be a quick solution without too much editing of the newsletter code)?
B. What RegEx would I need to use to just capture the first script?