Forum Moderators: coopster
The file is just a plain .txt file, which will contain a link like the following:
<!--Start--><a href="/path/to/page.html" title="Title of Link"><!--End-->
I need to be able to use everything between the Start and End comments as a variable.
I've been working with regular expressions, but can't seem to make them work. Here's some relevant code:
$SSI = "/path/to/file.txt";
$Handle = fopen($SSI, 'w+');
How can I use regular expressions to capture the necessary portion of the $SSI file?
extracting <title> contents from html files [webmasterworld.com]
Here's a bit more of what I'm trying to do:
$SSI = "/path/to/file.txt";
$Content = "Content to put in file";
$Handle = fopen($SSI, 'w+');
// The following line is what's causing the error:
$LastWeek = $Handle(ereg('(<!--Start-->)(.*)(<!--End-->)'));
$Content = "$Content $LastWeek";
$Write = fwrite($Handle, $Content))
The "$LastWeek" line is where I'm having a problem. I keep getting an error of "Fatal error: Call to undefined function: resource id #4() in /path/to/script.php on line 71" That sounds like I'm using the ereg function incorrectly, but I sure can't see a problem. Somehow I need to get all the content between those Start and End comments into a variable.
Thanks,
Matthew
$file into a variable, try: preg_match('#<!--Start-->(.*?)<!--End-->#', $file, $matches);
$found_comment = $matches[1]; The # are just the boundaries for the match. You can use any character except special characters. Calling preg_match [us2.php.net] will set the array, $matches. Everthing between the #...# should be in $matches[0]. What's within the (.*) should be in $matches[1]. the dot represents any character. The * means any number of the previous characters. The '?' makes the pattern "ungreedy." What this means is that if you had additional <!--End--> substrings later in the file, the regex's greediness would attempt to match everything up to the last one. The '?' makes it stop at the first one.
I hope this helps
$SSI = "/path/to/file.txt";
$Handle = fopen($SSI, 'a+');
$Contents = fread($Handle, filesize($SSI));
preg_match('#<!--Start-->(.*?)<!--End-->#', $Contents, $match);
$found_comment = $match[1];
echo "<p>$found_comment</p>";
Unfortunately that's still giving me the same undefined offset error message. I checked to make sure the file has the <!--Start--> and <!--End--> comments in it, and it does. So somehow things still aren't reading what comes between those comments.
$SSI = "file.txt";
if (!$Handle = fopen($SSI, 'a+')) die("Could not open file.");
if (!$Contents = fread($Handle, filesize($SSI))) die("Could not read file.");
if (!preg_match('#<!--Start-->(.*?)<!--End-->#', $Contents, $match)) die("preg_match found no match.");
if (!$found_comment = $match[1]) die("\$match[1] was not set.");
echo "<p>$found_comment</p>\n\n";