Jump to content


Photo

Backslash issue


  • Please log in to reply
9 replies to this topic

#1 benhodgson

benhodgson
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationLeicester, UK

Posted 25 October 2006 - 01:04 PM

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

#2 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 05:55 PM

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:

<?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";
  }
}
?>


#3 benhodgson

benhodgson
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationLeicester, UK

Posted 25 October 2006 - 06:16 PM

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:

Hello
Everybody

My name is Ben


...becomes...

Hello\Everybody\\My name is Ben


The script to transfer the formatted script into a .spt file is...

$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";
}


#4 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 07:09 PM

If you put in this code after you define $somecontent it should replace all newlines with a \

<?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);
?>

Is that what you were looking for?

#5 benhodgson

benhodgson
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationLeicester, UK

Posted 25 October 2006 - 07:20 PM

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.

\


As I say, stories just need to read like:

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.



#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 October 2006 - 07:45 PM

The previous poster had the wrong slash in the newline definition, try this:
<?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
?>

Ken

#7 benhodgson

benhodgson
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationLeicester, UK

Posted 25 October 2006 - 07:52 PM

Almost there, only problem now is that I'm getting two backslashes every time instead of one.

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 October 2006 - 07:54 PM

I edited the solution to move the "\r\n" to be first in the array. That fixes the problem.

Here's my test script:
<?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;
?>

Ken

#9 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 07:57 PM

If you do use single quotes, only use one \ rather than 2, since double quotes render \\ as \ and single quotes don't render anything

<?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
?>


#10 benhodgson

benhodgson
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationLeicester, UK

Posted 25 October 2006 - 08:04 PM

Brilliant, works perfectly. Really appreciated. Cheers for all your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users