Hightower Posted November 13, 2008 Posted November 13, 2008 (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 November 13, 2008 by Hightower Because I'm an idiot.
browolf Posted November 13, 2008 Posted November 13, 2008 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
Hightower Posted November 13, 2008 Author Posted November 13, 2008 Sorted it - I was looking for a kind of TRIM so I only have the domain name. PHP: parse_url - Manual - This did the job.
Hightower Posted November 13, 2008 Author Posted November 13, 2008 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?
tmcd35 Posted November 13, 2008 Posted November 13, 2008 $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'];
browolf Posted November 13, 2008 Posted November 13, 2008 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?
Hightower Posted November 13, 2008 Author Posted November 13, 2008 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........
Hightower Posted November 14, 2008 Author Posted November 14, 2008 Now I'm getting this with Toms answer.... OpenDNS[][]www.test2.com[][]www.test3.com (The square brackets represent a rectangle character in a text file.)
powdarrmonkey Posted November 14, 2008 Posted November 14, 2008 What web server and platform are you running, and what application and platform are you reading the file on?
tmcd35 Posted November 14, 2008 Posted November 14, 2008 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.
Hightower Posted November 14, 2008 Author Posted November 14, 2008 (edited) Dupe post - sorry Edited November 14, 2008 by Hightower Dupe post
Hightower Posted November 14, 2008 Author Posted November 14, 2008 This is what I've tried: (note [] means a square in a text file) "\n"www.test1.com[][]www.test2.com[][]www.test3.com "\r"www.test1.com[][]www.test2.com[][]www.test3.com "\r\n"www.test1.com OpenDNS test3.com "\n\r"www.test1.com[] []www.test2.com[] []www.test3.com[] The last one is close but I still get those stupid square characters that I don't want.
tom_newton Posted November 14, 2008 Posted November 14, 2008 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.
Hightower Posted November 14, 2008 Author Posted November 14, 2008 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?
tom_newton Posted November 14, 2008 Posted November 14, 2008 I was more thinking that it might contain a trailing newline, rather than trailing space. Is the server on unix or windows?
Hightower Posted November 14, 2008 Author Posted November 14, 2008 The server is a WAMP setup on Server 2003. The text file is stored in the same location. I'm going to try trimming the $url before entering the "\n"
Hightower Posted November 14, 2008 Author Posted November 14, 2008 I used the trim function - still got the same...
CESIL Posted November 14, 2008 Posted November 14, 2008 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.
Hightower Posted November 14, 2008 Author Posted November 14, 2008 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.
CESIL Posted November 14, 2008 Posted November 14, 2008 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.
Hightower Posted November 14, 2008 Author Posted November 14, 2008 Is it possible that it is part of the WAMP setup?
CESIL Posted November 14, 2008 Posted November 14, 2008 I have just tried the same code on my WAMP setup and it works ok...
Hightower Posted November 14, 2008 Author Posted November 14, 2008 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.
Hightower Posted November 18, 2008 Author Posted November 18, 2008 Ok, another thing that I would like to do with this script. Currently I'm pulling the domain name using PHP_HOST_URL which gives me (for example) Google if I enter http://www.google.com/pagehere What I would like to do is enter an address like http://www.google.com/pagehere and get google.com Anyone done this before or know how to do it?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now