benhodgson Posted October 25, 2006 Share Posted October 25, 2006 I'm trying to write a PHP script that will generate an ASCII file readable by a piece of software designed to display scripts for radio newsreading.The script file should look like this: http://www.demonfm.co.uk/news/good.sptUnfortunately the best I can get is this: http://www.demonfm.co.uk/news/script.sptI need it so that there is just a slash, not a slash and then a new line, as the software doesn't understand the new line and so anything after a slash at the moment doesn't show up.Also, if you save both of those pages as .spt files and then open them in notepad, you'll notice on comparison that there are some further issues in the php generated file.Does anyone have any idea how I can solve these issues? Any help is greatly appreciated.Thanks Quote Link to comment Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 First of all:you NEED to include code. I can't help you fix anything if I don't know how it's broken.Second:Where are you getting input from? How does your script know what to put for title and content.my best guess to generate what you are looking for would be:[code]<?php$title[50]=array(1=>'This is the story title');$content[50]=array(1=>'This is where the content goes.\As you can see, linebreaks are defined by a backslash.\\Double linebreaks by two.');$audiotitle[50][10]=array(1=>array(''));$audiosource[50][10]=array(1=>array(''));for($story=1;$story<50;$story++) { echo "[Story".$story."]\r\nTITLE=".$title[$story]."\r\nContent=".$content[$story]."\r\n"; for($audio=1;$audio<10;$audio++) { echo "AUDIO".$audio."_TITLE=".$audiotitle[$story][$audio]."\r\nAUDIO".$audio."_SOURCE=".$audiosource[$story][$audio]."\r\n"; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
benhodgson Posted October 25, 2006 Author Share Posted October 25, 2006 The information comes in through a form on another page using POST. All I really need now is some way of detecting a linebreak in the content input from the form and inserting a backslash. So that:[quote]HelloEverybodyMy name is Ben[/quote]...becomes...[quote]Hello\Everybody\\My name is Ben[/quote]The script to transfer the formatted script into a .spt file is...[code]$filename = 'script.spt';$somecontent = $script;if (is_writable($filename)) {if (!$handle = fopen ($filename, 'w')) {echo "The system cannot open the required file ($filename)";exit;}if (fwrite($handle, $somecontent) === FALSE) {echo "Cannot write to file ($filename)";exit;} echo "Update successful! News story submitted."; fclose($handle);} else {echo "Error! the file ($filename) is not writable";}[/code] Quote Link to comment Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 If you put in this code after you define $somecontent it should replace all newlines with a \[code]<?php$newline=array("/n","/r","/r/n"); //If you know which new line the page is giving you, you can replace it, but I don't remember which is the defualtstr_replace($newline,"\\",$somecontent);?>[/code]Is that what you were looking for? Quote Link to comment Share on other sites More sharing options...
benhodgson Posted October 25, 2006 Author Share Posted October 25, 2006 Doesn't seem to work, I just get linebreaks with that.The problem is that using a str_replace with "\\" as the replacement just always seems to create a backslash and then inserts a linebreak.[quote]\[/quote]As I say, stories just need to read like:[quote]TITLE=ThisCONTENT=And if ever there is a linebreak\Just like that one\\And that one. A backslash will magically appear instead of an actual linebreak.[/quote] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 25, 2006 Share Posted October 25, 2006 The previous poster had the wrong slash in the newline definition, try this:[code]<?php$newline=array("\r\n","\n","\r"); //If you know which new line the page is giving you, you can replace it, but I don't remember which is the default$somecontent = str_replace($newline,'\\',$somecontent); // notice the single quotes instead of the double quotes?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
benhodgson Posted October 25, 2006 Author Share Posted October 25, 2006 Almost there, only problem now is that I'm getting two backslashes every time instead of one. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 25, 2006 Share Posted October 25, 2006 I edited the solution to move the "\r\n" to be first in the array. That fixes the problem.Here's my test script:[code]<?php$content = "And if ever there is a linebreak\r\nJust like that one\r\n\r\nAnd that one. A backslash will magically appear instead of an actual linebreak.";$newline=array("\r\n","\n","\r"); //If you know which new line the page is giving you, you can replace it, but I don't remember which is the default$content = str_replace($newline,'\\',$content); // notice the single quotes instead of the double quotesecho $content;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 If you do use single quotes, only use one \ rather than 2, since double quotes render \\ as \ and single quotes don't render anything[code]<?php$newline=array("\r\n","\n","\r"); //If you know which new line the page is giving you, you can replace it, but I don't remember which is the default$somecontent = str_replace($newline,'\',$somecontent); // notice the single quotes instead of the double quotes?>[/code] Quote Link to comment Share on other sites More sharing options...
benhodgson Posted October 25, 2006 Author Share Posted October 25, 2006 Brilliant, works perfectly. Really appreciated. Cheers for all your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.