+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
Web Development Thread, Concatenate in PHP in Coding and Web Development; I have an internal web application I designed where a teacher adds a title and web url to a form. ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241

    Concatenate in PHP

    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:

    PHP Code:
    //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...
    Last edited by Hightower; 13th November 2008 at 03:00 PM. Reason: Because I'm an idiot.

  2. #2
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,524
    Thank Post
    106
    Thanked 88 Times in 74 Posts
    Blog Entries
    46
    Rep Power
    40
    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

  3. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    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.

  4. #4

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    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?

  5. #5

    tmcd35's Avatar
    Join Date
    Jul 2005
    Location
    Norfolk
    Posts
    5,620
    Thank Post
    845
    Thanked 883 Times in 731 Posts
    Blog Entries
    9
    Rep Power
    326
    Quote Originally Posted by Hightower View Post

    PHP Code:
    $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

    PHP Code:
    $stringData $_POST['txtAddress']."\n";
    fwrite($fh$stringData);
    fclose($fh); 
    or possibly

    PHP Code:
    $stringData "\n".$_POST['txtAddress']; 

  6. #6
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,524
    Thank Post
    106
    Thanked 88 Times in 74 Posts
    Blog Entries
    46
    Rep Power
    40
    Quote Originally Posted by Hightower View Post
    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?

  7. #7

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by tmcd35 View Post
    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

    PHP Code:
    $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:
    Hmmmmm........

  8. #8


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,462
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    try
    fwrite($fh,"\n");
    instead of \r\n

  9. #9

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    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.)

  10. #10

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    What web server and platform are you running, and what application and platform are you reading the file on?

  11. #11

    tmcd35's Avatar
    Join Date
    Jul 2005
    Location
    Norfolk
    Posts
    5,620
    Thank Post
    845
    Thanked 883 Times in 731 Posts
    Blog Entries
    9
    Rep Power
    326
    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.

  12. #12

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Dupe post - sorry
    Last edited by Hightower; 14th November 2008 at 10:34 AM. Reason: Dupe post

  13. #13

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    This is what I've tried: (note [] means a square in a text file)

    Code:
    "\n"
    www.test1.com[][]www.test2.com[][]www.test3.com

    Code:
    "\r"
    www.test1.com[][]www.test2.com[][]www.test3.com

    Code:
    "\r\n"
    www.test1.com

    OpenDNS

    test3.com

    Code:
    "\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.

  14. #14


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,462
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    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.

  15. #15

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    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?

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. PHP Help!
    By Cravon in forum Coding
    Replies: 2
    Last Post: 20th November 2007, 05:05 PM
  2. php no errors
    By browolf in forum Web Development
    Replies: 3
    Last Post: 23rd May 2007, 12:06 PM
  3. Help with PHP
    By Gatt in forum Web Development
    Replies: 1
    Last Post: 14th November 2006, 09:01 PM
  4. php help
    By beeswax in forum Windows
    Replies: 33
    Last Post: 14th October 2005, 04:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •