Asked  7 Months ago    Answers:  5   Viewed   32 times

I have a php parser that split a given string by line-breaks, doing something like this:

$lines = explode(PHP_EOL,$content);

The parser works fine when working on server side. However, when I pass the content via post by ajax (using jquery's $.post method) the problem arises: line breaks are not recogniezed. So after almost an hour of tests and head-aches I decided to changed PHP_EOL by "n" and it worked:

$lines = explode("n",$content);

Now it works! Damn it I lost so much time! Could somebody explain me when use PHP_EOL and "n" properly, so I can save time in the future? Appreciate your kind answers ;)



The constant PHP_EOL should generally be used for platform-specific output.

  • Mostly for file output really.
  • Actually the file functions already transform n ?? rn on Windows systems unless used in fopen(…, "wb") binary mode.

For file input you should prefer n however. While most network protocols (HTTP) are supposed to use rn, that's not guaranteed.

  • Therefore it's best to break up on n and remove any optional r manually:

    $lines = array_map("rtrim", explode("n", $content));

    Or use the file(…, FILE_IGNORE_NEW_LINES) function right away, to leave EOL handling to PHP or auto_detect_line_endings.

  • A more robust and terser alternative is using preg_split() and a regexp:

    $lines = preg_split("/R/", $content);

    The R placeholder detects any combination of r + n. So would be safest, and even work for Classic MacOS ? 9 text files (rarely seen in practice).

    Obligatory microoptimization note:
    While regex has a cost, it's surprisingly often speedier than manual loops and string postprocessing in PHP.

And there are a few classic examples where you should avoid PHP_EOL due to its platform-ambiguity:

  • Manual generation of network protocol payloads, such as HTTP over fsockopen().
  • For mail() and MIME construction (which really, you shouldn't do tediously yourself anyway).
  • File output, if you want to consistently write just Unix n newlines regardless of environment.

So use a literal "rn" combination when not writing to files, but preparing data for a specific context that expects network linebreaks.

Wednesday, March 31, 2021
answered 7 Months ago

Only "successful" controls are submitted. An unchecked checkbox or radio button is not "successful".

You need to declare a default value with a hidden input. Make sure the hidden input comes before the checkbox so if the checkbox is checked it will override the default hidden input since the names are the same:

<input name="p-sis-0110-1" type="hidden" value="0">
<input name="p-sis-0110-1" type="checkbox" value="1">

To use an array you need to explicitly define the indexes so they are the same:

<input name="input[0]" type="hidden" value="0">
<input name="input[0]" type="checkbox" value="1">
Wednesday, March 31, 2021
answered 7 Months ago

How do you know your script isn't receiving the data?

change success to the following:

success: function(data){
        $("#result").html('Submitted successfully:' + data);

All you are doing is saying 'if a response is received without an error update a div' - you have no way of knowing if your php script has run SUCCESSFULLY (it has run as you are getting success) or not at the moment.

with the 'data' in the function you can then see if information has been passed back by your script.

Try the above and let us know how you get on


PHP scripts when you access them are run ONCE - they have no memory (other than fixed variables such as $cheese = "yummy";

The reason you cannot see a response when you access the page (even after running your script) is that it is a bit stupid and doesn't remember you ran it.

So accessing the script directly the script is going

'Oh - it's show time, right.....err.....ok - has the person sent me 'bar' - nope well in that case I will not do what is inside the if statement - end of my job - nothing to do here I will send nothing back.'

Try this:

change $_POST['bar'] to $_GET['bar'];

Then go to your URL where the script is and type

You will notice you now get a response because you passed the script what it wanted to return true and so do what is inside the if statement.

For data to persist you need to store to a database or a text file.

Hope that is clear!

Saturday, May 29, 2021
answered 5 Months ago

The detail container is a FrameLayout because the Fragment that is displayed will be replaced using FragmentTransaction's replace() method.

The first argument to replace() is the ID of container whose Fragments will be replaced. If the FrameLayout in this example were replaced with a Fragment, then both the WorkStationListFragment and whatever detail Fragment is currently shown would be replaced by the new Fragment. By encapsulating the Fragment within a FrameLayout, you can replace just the details.

Tuesday, July 13, 2021
answered 3 Months ago

Generally for path related operations you should prefer NSURL over NSString because the path information can be stored more efficiently in NSURL (according to the class reference for NSFileManager). So I would recommend that for your APIs you use also NSURL.

Also NSURL has URLByAppendingPathComponent: and URLByAppendingPathExtension: so convenience is served as well :-)

Tuesday, August 3, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :