Jump to content

Backslash issue


benhodgson

Recommended Posts

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.spt

Unfortunately the best I can get is this: http://www.demonfm.co.uk/news/script.spt

I 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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]Hello
Everybody

My 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]
Link to comment
Share on other sites

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 defualt
str_replace($newline,"\\",$somecontent);
?>
[/code]

Is that what you were looking for?
Link to comment
Share on other sites

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=This
CONTENT=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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 quotes
echo $content;
?>[/code]

Ken
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.