|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (1 bit)
Join Date: Jan 2006
Posts: 1
|
How can I make my php script insert at a specific spot?
made a nifty little question and answer script on my website: http://www.thesmartass.info/index.php?main=oaq . It works by using fwrite to dump preformated html into a text file, then I include the text file into the site and manually answer the questions myself when they appear.
However it has really taken off and I have better plans for this script. To achieve them I need my script to output not to a text file, but to an Atom feed, I suppose rss would be alright as well.... At any rate, I have been attempting to make the script output to xml, however I can't figure out how to make my output insert in a specific spot in the atom feed. It only outputs at the end of the file, which causes the to cut off before the questions. How can I tell my output to appear directly after the first ID tag: <id>tag:thesmartass.info,2006:001</id> I tried to put it inside a preg_match_all tag, but to be honest I don't understand how that works at all so It didn't place it right. Before I added that, it just outputed $html and it landed at the bottom of a text file. Your help would be most appreciated, I have already asked at several other places and nobody has been able to fix the script, your site came highly reccomended. Code:
<?php
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
$phrases = array(
"is left to wonder",
"is pondering",
"wants to know",
"is curious to find out",
"is dying to know",
"desires the answer for",
"has been meditating on the answer to"
);
// write the question to a separate HTML file
$question = stripslashes($_POST["question"]);
if ($question == "?") $question = "";
if ($question) {
$author = ($_POST["name"]) ? $_POST["name"] : "Chicken ****";
$website = ($_POST["url"]) ? $_POST["url"] : "";
$filename = "aas.xml";
// build the message
if ($questions = fopen($filename, "r+t")) {
$QA = fread($questions, filesize($filename));
$id = preg_match_all("/tag:thesmartass.info,2006:00/", $QA, $match) + 1;
// add the entry
$html =
"\n".
"<entry>\n".
" <title>".htmlspecialchars($question)."</title>\n".
" <link href=\"http://thesmartass.info/index.php?main=oaq\" />\n".
" <id>tag:thesmartass.info,2006:00".$id."</id>\n".
" <updated>".date(DATE_ATOM)."</updated>\n".
" <contributor>\n".
" <name>".$author."</name>\n".
" <email>".$email."</email>\n".
" <foaf:topic rdf:resource=".$phrases[time() % count($phrases)].": />\n".
" <foaf:homepage rdf:resource=".$website." />\n".
" </contributor>\n".
" <content type=\"html\" xml:space=\"preserve\">\n".
" <![CDATA[ \n".
" <div class=\"answer\"></div>\n".
" ]]>\n".
" </content>\n".
"</entry>\n".
"\n";
$insertspot = preg_match_all("/<id>tag:thesmartass.info,2006:001</id>/", $html, $match);
$addhtml = "".$insertspot."\n".$html."";
fwrite($questions, $addhtml);
fclose($questions);
}
// send yourself an email
$to = "qna@thesmartass.info";
$subject = "New question on Ask-a-Smartass page";
$contents = "Question #".$id.": ".$question."\n\n".$author."\n".$email."\n".$website."\n".$REMOTE_ADDR."\n";
$mailheaders = "From: $author <$email> \n";
$mailheaders .= "Reply-To: $email\n\n";
@mail($to, $subject, $contents, $mailheaders);
}
?>
|
|
|
|
|
|
#2 |
|
Come in Ray...
Join Date: Sep 2004
Posts: 1,668
|
Read the file into a string using the file_get_contents function. Then you can use the string/xml functions to parse it.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|