Stupid PHP Code Question...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Webmaster Advertising
    So Fucking Banned
    • Sep 2003
    • 1360

    #1

    Stupid PHP Code Question...

    Im getting this error on an old php file im trying to migrate across to a new server...

    http://www.pixelsquaredesigns.com/words/

    Any of the programmer types on here know whats causing it?

    Here is the code...

    Code:
    <?php
    
    // Text file containing words 
    $txt = '/home/orange/public_html/pixelsquaredesigns.com/words/list.txt'; 
    
    $handle = fopen($txt, 'r'); 
    
    if (!$handle) { 
       die('Could not open...'); 
    } 
    
    // set up arrays for storing words and concatenated words 
    $words = array(); 
    $cwords = array(); 
    
    // get the words 
    while (!feof($handle)) { 
       // strip_newlines is not a real function - needs to be implemented 
       array_push($words, strip_newlines(fgets($handle))); 
    } 
    
    // while we still have words 
    for ($i = 0; $i < count($words); $i++) { 
       // get the current word 
       $curword = $words[$i]; 
       // loop over the words 
       foreach ($words as $word) { 
          // if the word is the same as the current word, skip it - i.e. no CatCat 
          if ($word == $curword) { 
             continue; 
          } 
          // append the concatenated word to the array 
          array_push($cwords, $curword.$word); 
        } 
    } 
    
    foreach ($words as $var) echo $var."<br>";
    foreach ($cwords as $var2) echo $var2."<br>";
    
    ?>
    Thanks in advance for any assistance you can offer
  • CYF
    Coupon Guru
    • Mar 2009
    • 10973

    #2
    Fatal error: Call to undefined function strip_newlines() in /home/orange/public_html/pixelsquaredesigns.com/words/index.php on line 19

    I'm going to guess that the strip_newlines function isn't defined

    Your code says " // strip_newlines is not a real function - needs to be implemented " so I'm guessing the function wasn't added.
    Last edited by CYF; 12-13-2012, 09:52 PM.
    Webmaster Coupons Coupons and discounts for hosting, domains, SSL Certs, and more!
    AmeriNOC Coupons | Certified Hosting Coupons | Hosting Coupons | Domain Name Coupons

    Comment

    • borked
      Totally Borked
      • Feb 2005
      • 6284

      #3
      as CYF says
      looks like your old server had display errors turned off in php.ini (which you should have for a production server) so you were not seeing this error.

      For coding work - hit me up on andy // borkedcoder // com
      (consider figuring out the email as test #1)



      All models are wrong, but some are useful. George E.P. Box. p202

      Comment

      • Webmaster Advertising
        So Fucking Banned
        • Sep 2003
        • 1360

        #4
        So how would i fix it? I know it used to work on the old server with no problems

        Comment

        • Webmaster Advertising
          So Fucking Banned
          • Sep 2003
          • 1360

          #5
          So ive edited some of the code (new code is in red) but its now throwing out a different error

          http://www.pixelsquaredesigns.com/words/

          Code:
          <?php
          
          // Text file containing words 
          $txt = '/home/orange/public_html/pixelsquaredesigns.com/words/list.txt'; 
          
          $handle = fopen($txt, 'r'); 
          
          if (!$handle) { 
             die('Could not open...'); 
          } 
          
          // set up arrays for storing words and concatenated words 
          $words = array(); 
          $cwords = array(); 
          
          // get the words 
          while (!feof($handle)) { 
          [COLOR="Red"]   function strip_newlines($str) {
           // common newline sequences - \r (carriage return), \n (newline or linefeed), and \r\n (carriage return-linefeed)
           $newlines = array(0 => '\r', 1 => '\n', 2 => '\r\n');
           // loop over the newline sequences, replacing them with nothing in the string
           foreach ($newlines as $nl) {
           $str = str_replace($nl, '', $str);
           }
           // return the cleaned string
           return $str;
           }[/COLOR]
          
          // while we still have words 
          for ($i = 0; $i < count($words); $i++) { 
             // get the current word 
             $curword = $words[$i]; 
             // loop over the words 
             foreach ($words as $word) { 
                // if the word is the same as the current word, skip it - i.e. no CatCat 
                if ($word == $curword) { 
                   continue; 
                } 
                // append the concatenated word to the array 
                array_push($cwords, $curword.$word); 
              } 
          } 
          
          foreach ($words as $var) echo $var."<br>";
          foreach ($cwords as $var2) echo $var2."<br>";
          
          ?>
          What did I screw up? LOL

          Sorry I'm definitely not fluent in .php but I'm learning slowly... Or trying to lol

          Comment

          • cemtex
            Confirmed User
            • Nov 2006
            • 289

            #6
            Code:
            <?php
            
            // Text file containing words 
            $txt = 'list.txt'; 
            
            
            // function
            function strip_newlines($str) {
            	// common newline sequences - \r (carriage return), \n (newline or linefeed), and \r\n (carriage return-linefeed)
            	$newlines = array(0 => '\r', 1 => '\n', 2 => '\r\n');
            	// loop over the newline sequences, replacing them with nothing in the string
            		foreach ($newlines as $nl) {
            		$str = str_replace($nl, '', $str);
            		}
            	// return the cleaned string
            	return $str;
            }
            
            $handle = fopen($txt, 'r'); 
            
            if (!$handle) { 
               die('Could not open...'); 
            } 
            
            // set up arrays for storing words and concatenated words 
            $words = array(); 
            $cwords = array(); 
            
            // get the words 
            while (!feof($handle)) { 
               // strip_newlines is not a real function - needs to be implemented 
               array_push($words, strip_newlines(fgets($handle))); 
            } 
            
            // while we still have words 
            for ($i = 0; $i < count($words); $i++) { 
            // get the current word 
            $curword = $words[$i]; 
            // loop over the words 
            	foreach ($words as $word) { 
            		// if the word is the same as the current word, skip it - i.e. no CatCat 
            		if ($word == $curword) { 
            		 continue; 
            		} 
            	// append the concatenated word to the array 
            	array_push($cwords, $curword.$word); 
            	} 
            } 
            
            foreach ($words as $var) echo $var."<br>";
            foreach ($cwords as $var2) echo $var2."<br>";
            
            ?>

            Comment

            • VenusBlogger
              So Fucking Banned
              • Nov 2011
              • 1540

              #7
              Ask KILLSWITCH or the so Fucking Banned "EDGEPROD", HO HO HO.

              They claim theirselves to be PHP NINJAS...

              lol.. they dont have a fucking clue about coding at all.. Fucking noobs.

              Comment

              • Emil
                Confirmed User
                • Feb 2007
                • 5658

                #8
                http://fiverr.com/gigs/search?query=fix+php&x=0&y=0
                Free 🅑🅘🅣🅒🅞🅘🅝🅢 Every Hour (Yes, really. Free ₿itCoins.)
                (Signup with ONLY your Email and Password. You can also refer people and get even more.)

                Comment

                • amphora
                  Registered User
                  • Nov 2012
                  • 1

                  #9
                  That new error, "Parse error: syntax error, unexpected $end", means you are missing a closing bracket. A right curly bracket, } needs to match up with one of the opening ones, {.

                  Another problem with your new code is although you have now defined a strip_newlines function, it is never actually called, plus it is being defined in a loop for no reason.

                  Those were the problems, cemtex's code should get you on track.

                  Comment

                  • Webmaster Advertising
                    So Fucking Banned
                    • Sep 2003
                    • 1360

                    #10
                    Originally posted by cemtex
                    Code:
                    <?php
                    
                    // Text file containing words 
                    $txt = 'list.txt'; 
                    
                    
                    // function
                    function strip_newlines($str) {
                    	// common newline sequences - \r (carriage return), \n (newline or linefeed), and \r\n (carriage return-linefeed)
                    	$newlines = array(0 => '\r', 1 => '\n', 2 => '\r\n');
                    	// loop over the newline sequences, replacing them with nothing in the string
                    		foreach ($newlines as $nl) {
                    		$str = str_replace($nl, '', $str);
                    		}
                    	// return the cleaned string
                    	return $str;
                    }
                    
                    $handle = fopen($txt, 'r'); 
                    
                    if (!$handle) { 
                       die('Could not open...'); 
                    } 
                    
                    // set up arrays for storing words and concatenated words 
                    $words = array(); 
                    $cwords = array(); 
                    
                    // get the words 
                    while (!feof($handle)) { 
                       // strip_newlines is not a real function - needs to be implemented 
                       array_push($words, strip_newlines(fgets($handle))); 
                    } 
                    
                    // while we still have words 
                    for ($i = 0; $i < count($words); $i++) { 
                    // get the current word 
                    $curword = $words[$i]; 
                    // loop over the words 
                    	foreach ($words as $word) { 
                    		// if the word is the same as the current word, skip it - i.e. no CatCat 
                    		if ($word == $curword) { 
                    		 continue; 
                    		} 
                    	// append the concatenated word to the array 
                    	array_push($cwords, $curword.$word); 
                    	} 
                    } 
                    
                    foreach ($words as $var) echo $var."<br>";
                    foreach ($cwords as $var2) echo $var2."<br>";
                    
                    ?>
                    Thank you kindly, its appreciated.

                    Like i said, I'm learning php slowly, i knew there was an issue just couldnt 'see' what it was in my head.

                    So i was basically screwing up the handling of strip_newlines forcing the array in there, correct?

                    Comment

                    • Vapid - BANNED FOR LIFE
                      Barterer
                      • Aug 2004
                      • 4864

                      #11
                      Layer 7 firewall

                      Comment

                      • Webmaster Advertising
                        So Fucking Banned
                        • Sep 2003
                        • 1360

                        #12
                        Originally posted by amphora
                        That new error, "Parse error: syntax error, unexpected $end", means you are missing a closing bracket. A right curly bracket, } needs to match up with one of the opening ones, {.

                        Another problem with your new code is although you have now defined a strip_newlines function, it is never actually called, plus it is being defined in a loop for no reason.

                        Those were the problems, cemtex's code should get you on track.
                        Thank you for the explanation

                        Comment

                        • Killswitch
                          REVOLUTIONARY
                          • Oct 2012
                          • 2567

                          #13
                          Originally posted by VenusBlogger
                          Ask KILLSWITCH or the so Fucking Banned "EDGEPROD", HO HO HO.

                          They claim theirselves to be PHP NINJAS...

                          lol.. they dont have a fucking clue about coding at all.. Fucking noobs.

                          Comment

                          • VenusBlogger
                            So Fucking Banned
                            • Nov 2011
                            • 1540

                            #14
                            HAHA, funny that KILLSWITCH thinks I was referring to GFY...

                            Im laughing my ass off...

                            DOMINICO is definitely banned from that place, I had one of my friends member of the forum check on it.

                            Same as I have him gathering all the posts I cannot read, every single day... So I stay up to date and I know exactly what you post and that someone opened a thread...

                            Don't worry, Im always watching.

                            Take it with Lube.
                            Last edited by VenusBlogger; 12-14-2012, 03:24 PM.

                            Comment

                            • Killswitch
                              REVOLUTIONARY
                              • Oct 2012
                              • 2567

                              #15
                              Originally posted by VenusBlogger
                              HAHA, funny that KILLSWITCH thinks I was referring to GFY...

                              Im laughing my ass off...

                              DOMINICO is definitely banned from that place, I had one of my friends member of the forum check on it.

                              Same as I have him gathering all the posts I cannot read, every single day... So I stay up to date and I know exactly what you post and that someone opened a thread...

                              Don't worry, Im always watching.

                              Take it with Lube.
                              Your rat is filling you with bullshit.

                              Comment

                              • VenusBlogger
                                So Fucking Banned
                                • Nov 2011
                                • 1540

                                #16
                                Cada vez que busco tu nombre en google, me rio a carcajadas...

                                Reputation destroyed.

                                y el proximo afectado sera edgeprod, el sicko de conectivut... hihihi...

                                yup, saw him banned yesterday on that other place..

                                dont worry, I dont follow ur game.
                                Last edited by VenusBlogger; 12-14-2012, 03:49 PM.

                                Comment

                                • edgeprod
                                  Permanently Gone
                                  • Mar 2004
                                  • 10019

                                  #17
                                  Originally posted by Killswitch
                                  Your rat is filling you with bullshit.
                                  I'm laughing my balls off, because whoever is feeding him bad info, he probably thinks they're friends ... and the whole time .. oh, god I have tears in my eyes, I'm laughing so hard. Totally worth it.

                                  Comment

                                  • Killswitch
                                    REVOLUTIONARY
                                    • Oct 2012
                                    • 2567

                                    #18
                                    Originally posted by edgeprod
                                    I'm laughing my balls off, because whoever is feeding him bad info, he probably thinks they're friends ... and the whole time .. oh, god I have tears in my eyes, I'm laughing so hard. Totally worth it.

                                    Comment

                                    • edgeprod
                                      Permanently Gone
                                      • Mar 2004
                                      • 10019

                                      #19
                                      You know, something just occurred to me: if he WAS having access to that forum, he'd see me posting on it every day, so ... I guess he got caught in a lie either way. Ah well, it's not like he had credibility. GFY sure is nice without seeing his posts, using your ignore tool .. but if you keep QUOTING him, we still have to see THAT stuff, lol.

                                      Comment

                                      • Killswitch
                                        REVOLUTIONARY
                                        • Oct 2012
                                        • 2567

                                        #20
                                        Originally posted by edgeprod
                                        You know, something just occurred to me: if he WAS having access to that forum, he'd see me posting on it every day, so ... I guess he got caught in a lie either way. Ah well, it's not like he had credibility. GFY sure is nice without seeing his posts, using your ignore tool .. but if you keep QUOTING him, we still have to see THAT stuff, lol.
                                        My good your bad. How's it working on your fourth computer since it RUINED your first THREE computers?

                                        Comment

                                        • edgeprod
                                          Permanently Gone
                                          • Mar 2004
                                          • 10019

                                          #21
                                          Originally posted by Killswitch
                                          My good your bad. How's it working on your fourth computer since it RUINED your first THREE computers?
                                          My first 43 computers. I just kept trying, and your damn script just kept RUINING computers. I had to actually throw them all out afterward. If I weren't in a third world country running a 486 DX, I'd ask you to replace at least one of them with a smoke signal station or a messenger pigeon, KILL-SWITCH.

                                          Also: I HAVE REPORTED YOU TO THE FBI TEAM, THE GOOGLES, GIT-HUBBLE, AND FACE-BOOKING (where you have only 6 likes, how can you call yourself a WEBMATADOR)?

                                          Comment

                                          • Killswitch
                                            REVOLUTIONARY
                                            • Oct 2012
                                            • 2567

                                            #22
                                            Originally posted by edgeprod
                                            My first 43 computers. I just kept trying, and your damn script just kept RUINING computers. I had to actually throw them all out afterward. If I weren't in a third world country running a 486 DX, I'd ask you to replace at least one of them with a smoke signal station or a messenger pigeon, KILL-SWITCH.

                                            Also: I HAVE REPORTED YOU TO THE FBI TEAM, THE GOOGLES, GIT-HUBBLE, AND FACE-BOOKING (where you have only 6 likes, how can you call yourself a WEBMATADOR)?
                                            Just for this post I have banned you from GFY. Enjoy your SO FUCKING BANNED status, EDGE-PROD!

                                            Comment

                                            • edgeprod
                                              Permanently Gone
                                              • Mar 2004
                                              • 10019

                                              #23
                                              Originally posted by Killswitch
                                              Just for this post I have banned you from GFY. Enjoy your SO FUCKING BANNED status, EDGE-PROD!
                                              You FATTY, I will call you a KICK-BOXER now.

                                              Comment

                                              • Killswitch
                                                REVOLUTIONARY
                                                • Oct 2012
                                                • 2567

                                                #24
                                                Originally posted by edgeprod
                                                You FATTY, I will call you a KICK-BOXER now.
                                                You can't respond to this because you're SO FUCKING BANNED!

                                                Comment

                                                • Webmaster Advertising
                                                  So Fucking Banned
                                                  • Sep 2003
                                                  • 1360

                                                  #25
                                                  So this is doing what it is supposed to now but i had one more question, and im not sure if its possible or not from what ive been reading, i can replace the string for a blank space however, i also read it only works for white space at the beginning or end of a word.

                                                  Is there any way i can have this script output a list like this....

                                                  onetwo
                                                  onethree
                                                  twoone
                                                  twothree

                                                  Instead of like this how it currently does...

                                                  one two
                                                  one three
                                                  two one
                                                  two three

                                                  If so, what would i put in the code and where would i put it?

                                                  Thanks for all the assistance so far

                                                  Comment

                                                  • edgeprod
                                                    Permanently Gone
                                                    • Mar 2004
                                                    • 10019

                                                    #26
                                                    Originally posted by Killswitch
                                                    You can't respond to this because you're SO FUCKING BANNED!
                                                    I forgot.

                                                    Comment

                                                    • Killswitch
                                                      REVOLUTIONARY
                                                      • Oct 2012
                                                      • 2567

                                                      #27
                                                      Originally posted by Webmaster Advertising
                                                      So this is doing what it is supposed to now but i had one more question, and im not sure if its possible or not from what ive been reading, i can replace the string for a blank space however, i also read it only works for white space at the beginning or end of a word.

                                                      Is there any way i can have this script output a list like this....

                                                      onetwo
                                                      onethree
                                                      twoone
                                                      twothree

                                                      Instead of like this how it currently does...

                                                      one two
                                                      one three
                                                      two one
                                                      two three

                                                      If so, what would i put in the code and where would i put it?

                                                      Thanks for all the assistance so far
                                                      str_replace(' ', '', $var);

                                                      Comment

                                                      • Webmaster Advertising
                                                        So Fucking Banned
                                                        • Sep 2003
                                                        • 1360

                                                        #28
                                                        Ah, it was the $var I was missing... Thank you kindly Sir

                                                        Comment

                                                        • Webmaster Advertising
                                                          So Fucking Banned
                                                          • Sep 2003
                                                          • 1360

                                                          #29
                                                          Im still screwing something up

                                                          http://www.pixelsquaredesigns.com/words/

                                                          Has a bunch of spaces between the words, except for the last 4 lines now :/

                                                          Comment

                                                          • sarettah
                                                            see you later, I'm gone
                                                            • Oct 2002
                                                            • 14297

                                                            #30
                                                            Originally posted by Webmaster Advertising
                                                            Im still screwing something up

                                                            http://www.pixelsquaredesigns.com/words/

                                                            Has a bunch of spaces between the words, except for the last 4 lines now :/
                                                            You have something other than a space in there.

                                                            So, instead of just doing the replace do a trim when you put the words together.

                                                            Here:

                                                            array_push($cwords, $curword . $word);

                                                            Change to

                                                            array_push($cwords, trim($curword) . trim($word));

                                                            or

                                                            Here:

                                                            array_push($words, strip_newlines(fgets($handle)));

                                                            change to

                                                            array_push($words, trim(strip_newlines(fgets($handle))));

                                                            Trim takes white space (not just blanks) from the beginning and end of the string.

                                                            Edited in:

                                                            A better place for it is in the strip_newlines function actually.

                                                            Changing return $str; to return trim($str); will accomplish the exact same thing with slightly less typing. It depends on how you would interpret what you want the strip_newlines function to be as to whether it is in the right place.
                                                            Last edited by sarettah; 12-14-2012, 08:28 PM.
                                                            All cookies cleared!

                                                            Comment

                                                            • sarettah
                                                              see you later, I'm gone
                                                              • Oct 2002
                                                              • 14297

                                                              #31
                                                              Looking at it, I don't like the strip_newlines function in there either...lol.

                                                              I always use chr(13) and chr(10) for my new line indicators and it never fails.

                                                              So, on the read (the fget) you can do a replace of all of it and get rid of the function.

                                                              changing the read to:

                                                              array_push($words, str_replace(chr(13),'',str_replace(chr(10), '', fgets($handle))));

                                                              and getting rid of the function should take care of it nicely. If you also want rid of spaces and such (just in case) then:

                                                              array_push($words, trim(str_replace(chr(13),'',str_replace(chr(10), '', fgets($handle)))));

                                                              Should take care of it.
                                                              All cookies cleared!

                                                              Comment

                                                              • Webmaster Advertising
                                                                So Fucking Banned
                                                                • Sep 2003
                                                                • 1360

                                                                #32
                                                                Originally posted by sarettah
                                                                You have something other than a space in there.

                                                                So, instead of just doing the replace do a trim when you put the words together.

                                                                Here:

                                                                array_push($cwords, $curword . $word);

                                                                Change to

                                                                array_push($cwords, trim($curword) . trim($word));

                                                                or

                                                                Here:

                                                                array_push($words, strip_newlines(fgets($handle)));

                                                                change to

                                                                array_push($words, trim(strip_newlines(fgets($handle))));

                                                                Trim takes white space (not just blanks) from the beginning and end of the string.

                                                                Edited in:

                                                                A better place for it is in the strip_newlines function actually.

                                                                Changing return $str; to return trim($str); will accomplish the exact same thing with slightly less typing. It depends on how you would interpret what you want the strip_newlines function to be as to whether it is in the right place.
                                                                Trim worked perfectly, thanks Sarettah.

                                                                Stupid question though, if this was just a list of words typed in notepad, one word per line, how come there was a space in there, is that an issue with the server parsing the array or something notepad related?

                                                                Comment

                                                                • sarettah
                                                                  see you later, I'm gone
                                                                  • Oct 2002
                                                                  • 14297

                                                                  #33
                                                                  Originally posted by Webmaster Advertising
                                                                  Trim worked perfectly, thanks Sarettah.

                                                                  Stupid question though, if this was just a list of words typed in notepad, one word per line, how come there was a space in there, is that an issue with the server parsing the array or something notepad related?
                                                                  There was no space.

                                                                  Your strip_newlines function is NOT stripping properly. I just did a couple of tests just to prove it to myself. There was still a carriage return and linefeed in there.

                                                                  Too loopy right now to look through the function. I say get rid of it because it is unneeded.

                                                                  If you go back to the code before the trim and look at view source you will see that what in html looked like a space was actual a skip down a line. But since html does not use carriage returns (it uses the <br> tag) you do not see it in the browser.

                                                                  Handling the carriage return/line feeds using the chr (character function) takes care of it.
                                                                  All cookies cleared!

                                                                  Comment

                                                                  • sarettah
                                                                    see you later, I'm gone
                                                                    • Oct 2002
                                                                    • 14297

                                                                    #34
                                                                    Ok, the issue in the strip_newlines.

                                                                    The function as shown above is using single ticks (apostrophes) for the variables:

                                                                    newlines = array(0 => '\r', 1 => '\n', 2 => '\r\n');

                                                                    Those need to be in double tics (quotes):

                                                                    newlines = array(0 => "\r", 1 => "\n", 2 => "\r\n");

                                                                    Then, it will work. But there is a condition that will NEVER get hit. The third "\r\n" will never be encountered so it can be left out and you could use

                                                                    newlines = array(0 => "\r", 1 => "\n");

                                                                    That would work properly and give the proper result.



                                                                    .
                                                                    All cookies cleared!

                                                                    Comment

                                                                    • Webmaster Advertising
                                                                      So Fucking Banned
                                                                      • Sep 2003
                                                                      • 1360

                                                                      #35
                                                                      Okay cool ill have a play with this again in the morning then, time to get mah drink on right now, thank you again for the help

                                                                      Comment

                                                                      • VenusBlogger
                                                                        So Fucking Banned
                                                                        • Nov 2011
                                                                        • 1540

                                                                        #36
                                                                        Originally posted by edgeprod
                                                                        I forgot.
                                                                        EDGE-PROOF why is your profile in GAY DATING Sites along with your wikipidia pic ?

                                                                        I didn't know you were a homo.

                                                                        And something tells me that IF I keep searching, I can also find KILLSWITCH profil too...

                                                                        Right JOSHUA TREE ?

                                                                        KARMA is a bitch!

                                                                        Comment

                                                                        • Killswitch
                                                                          REVOLUTIONARY
                                                                          • Oct 2012
                                                                          • 2567

                                                                          #37

                                                                          Comment

                                                                          • edgeprod
                                                                            Permanently Gone
                                                                            • Mar 2004
                                                                            • 10019

                                                                            #38
                                                                            Well, signing us up for gay dating profiles is hil-fucking-arious and all, but it's nice to see him banned, too.

                                                                            Comment

                                                                            • Killswitch
                                                                              REVOLUTIONARY
                                                                              • Oct 2012
                                                                              • 2567

                                                                              #39
                                                                              Damn, I've been out all day helping my buddy buy an engagement ring... What did I miss? What caused him to be banned?

                                                                              Comment

                                                                              Working...