Jump to content

Recommended Posts

Posted (edited)

I have an internal web application I designed where a teacher adds a title and web url to a form. This is then stored in a database. The web url is also stored in a text file.

 

I want the database to store the full address i.e.

 

http://www.rm.com/somewebdirhere/somewebpage.ext

 

But I want the text file to only store:

 

RM - Exciting Learning

 

Can anybody help me concatenate this? This is the code I use to write the url to the text file at the minute:

 

//This adds the address submitted to a text file
//ready for the proxy server to use
$myFile = "whitelist.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['txtAddress'];
fwrite($fh, $stringData);
fwrite($fh, "\r\n");
fclose($fh);

 

EDIT: I don't think the term is concatenate but I'm sure you know what I mean...

Edited by Hightower
Because I'm an idiot.
Posted

concatenate is when you want to join strings together...

 

u mean u just want the http:// domain / ?

 

if you split the string on on the slashes, u know u want everything before and including the 3rd slash

Posted

Another problem I'm getting though based on the code above, is my text file has multiple lines in between each entry when I don't want any. Just want one carriage return like this:

 

Entry 1

Entry 2

Entry 3

 

Not like this (which is how its working at the min)

 

Entry 1

 

Entry 2

 

 

Entry 3

 

 

It doesnt hurt - the text file just looks messy. Any ideas?

Posted

 

$stringData = $_POST['txtAddress'];
fwrite($fh, $stringData);
fwrite($fh, "\r\n");
fclose($fh);

 

 

I think the problem is the '\r' escape code. I think it's an additional, not needed carriage return which is causing the problem. '\n' on it's own should work.

 

I'd rewrite thus

 

$stringData = $_POST['txtAddress']."\n";
fwrite($fh, $stringData);
fclose($fh);

 

or possibly

 

$stringData = "\n".$_POST['txtAddress'];

Posted
Another problem I'm getting though based on the code above, is my text file has multiple lines in between each entry when I don't want any. Just want one carriage return like this:

 

Entry 1

Entry 2

Entry 3

 

Not like this (which is how its working at the min)

 

Entry 1

 

Entry 2

 

 

Entry 3

 

 

It doesnt hurt - the text file just looks messy. Any ideas?

 

wont trim sort of thing fix that too?

Posted
I think the problem is the '\r' escape code. I think it's an additional, not needed carriage return which is causing the problem. '\n' on it's own should work.

 

I'd rewrite thus

 

$stringData = $_POST['txtAddress']."\n";
fwrite($fh, $stringData);
fclose($fh);

or possibly

 

Thanks but didn't work - I've just added to address' and it appeared like this in the text file:

[url="http://www.edugeek.netwww.rm.com"]www.edugeek.netwww.rm.com[/url]

Hmmmmm........

Posted

Try replacing '\n' with '\r'

 

Alwaysed used '\n' for new line before and never had a problem. I'm guessing '\r' is a carriage return code. never used it before. I think your original problem was becaude you where using both, I think needed for Windows .txt files. On that logic. If '\n' doesn't work, try '\r' on it's own.

Posted

I would suggest that there may be trailing characters on your input - worth checking.

Seems a lot of your probs could be windows/unix related - remember the two OS's terminate lines differently. If this file si not meant to be human readable, of course, it does not matter, you could put nulls or tabs, or anything between em.

Posted

The file needs a line break for the server to read each line properly. It kind of needs to be readable in case it needs to be edited.

 

When you say trailing characters do you mean something like the web address might look like ____www.rm.com____ (where _ is a space)?

 

Should I run the trim function on this?

Posted

What editor are you viewing the file in?

 

Notepad does not recognize *nix style returns (\n) it needs \n\r

 

Textpad reads *nix files properly so that is what I use.

Posted

It doesn't matter which editor I use. Windows Text Editor puts [][], Notepad++ and others don't but there is still more than one line break.

 

\n puts in [][] or 2 line breaks (depending on text editor)

 

\r puts in [][][] or 3 line breaks (depending on text editor)

 

It works - I just want it to look tidier rather than loads of un-needed whitespace.

Posted

Can you post the code you are now using?

 

I have just tried the code you posted originally and it is producing a perfect single line spaced file.

Posted

So it's something caused in another area of the code? Or something caused within my WAMP setup?

 

I'll post the rest of the code up later - but can't really see how. It must be WAMP.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...