Viewed   1.5k times

I am hesitated to ask this question because it looks weird. But anyway. Just in case someone had encountered the same problem already... filesystem functions (fopem, file, file_get_contents) behave very strange for http:// wrapper

  • it seemingly works. no errors raised. fopen() returns resource.
  • it returns no data for all certainly working urls (e.g.
    file returns empty array, file_get_contents() returns empty string, fread returns false
  • for all intentionally wrong urls (e.g. it behaves exactly the same, save for little [supposedly domain lookup] timeout, after which I get no error (while should!) but empty string.
  • url_fopen_wrapper is turned on
  • curl (both command line and php versions) works fine, all other utilities and applications works fine, local files opened fine

This error seems inapplicable because in my case it doesn't work for every url or host.

php-fpm 5.2.11 Linux version (



I fixed this issue on my server (running PHP 5.3.3 on Fedora 14) by removing the --with-curlwrapper from the PHP configuration and rebuilding it.

Thursday, September 8, 2022


foreach($matches[1] as $content)
     echo $content."rn";

only iterates over the array and makes $content the last element (you have no {} so it is a one liner).

Simple demo of your issue,

You could use use implode though.

fwrite($file,implode("nr", $matches[1]));

You also could simplify this by using file_put_contents. Per the manual:

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

So you could just do:

$re = '/<li><a href="(.*?)"/';
$str = '
<li><a href=""</a></li>
                        <li><a href=""</a></li>
                        <li><a href=""</a></li> ';

preg_match_all($re, $str, $matches);
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
file_put_contents("1.txt", implode("nr", $matches[1]));
Saturday, November 12, 2022

Thanks CBroe and IVO GELOV, The issue was in my FTP/SFTP Client WinSCP. I attached a screenshot which setting you need to turn off so this problem doesn't happen.

Sunday, December 18, 2022

Question: I am using split('n') to get lines in one string, and found that ''.split() returns an empty list, [], while ''.split('n') returns [''].

The str.split() method has two algorithms. If no arguments are given, it splits on repeated runs of whitespace. However, if an argument is given, it is treated as a single delimiter with no repeated runs.

In the case of splitting an empty string, the first mode (no argument) will return an empty list because the whitespace is eaten and there are no values to put in the result list.

In contrast, the second mode (with an argument such as n) will produce the first empty field. Consider if you had written 'n'.split('n'), you would get two fields (one split, gives you two halves).

Question: Is there any specific reason for such a difference?

This first mode is useful when data is aligned in columns with variable amounts of whitespace. For example:

>>> data = '''
Shasta      California     14,200
McKinley    Alaska         20,300
Fuji        Japan          12,400
>>> for line in data.splitlines():

['Shasta', 'California', '14,200']
['McKinley', 'Alaska', '20,300']
['Fuji', 'Japan', '12,400']

The second mode is useful for delimited data such as CSV where repeated commas denote empty fields. For example:

>>> data = '''
>>> for line in data.splitlines():

['Guido', 'BDFL', '', 'Amsterdam']
['Barry', 'FLUFL', '', 'USA']
['Tim', '', '', 'USA']

Note, the number of result fields is one greater than the number of delimiters. Think of cutting a rope. If you make no cuts, you have one piece. Making one cut, gives two pieces. Making two cuts, gives three pieces. And so it is with Python's str.split(delimiter) method:

>>> ''.split(',')       # No cuts
>>> ','.split(',')      # One cut
['', '']
>>> ',,'.split(',')     # Two cuts
['', '', '']

Question: And is there any more convenient way to count lines in a string?

Yes, there are a couple of easy ways. One uses str.count() and the other uses str.splitlines(). Both ways will give the same answer unless the final line is missing the n. If the final newline is missing, the str.splitlines approach will give the accurate answer. A faster technique that is also accurate uses the count method but then corrects it for the final newline:

>>> data = '''
Line 1
Line 2
Line 3
Line 4'''

>>> data.count('n')                               # Inaccurate
>>> len(data.splitlines())                         # Accurate, but slow
>>> data.count('n') + (not data.endswith('n'))   # Accurate and fast

Question from @Kaz: Why the heck are two very different algorithms shoe-horned into a single function?

The signature for str.split is about 20 years old, and a number of the APIs from that era are strictly pragmatic. While not perfect, the method signature isn't "terrible" either. For the most part, Guido's API design choices have stood the test of time.

The current API is not without advantages. Consider strings such as:

ps_aux_header  = 'USER               PID  %CPU %MEM      VSZ'
patient_header = 'name,age,height,weight'

When asked to break these strings into fields, people tend to describe both using the same English word, "split". When asked to read code such as fields = line.split() or fields = line.split(','), people tend to correctly interpret the statements as "splits a line into fields".

Microsoft Excel's text-to-columns tool made a similar API choice and incorporates both splitting algorithms in the same tool. People seem to mentally model field-splitting as a single concept even though more than one algorithm is involved.

Saturday, October 1, 2022

Why are you using String.split() for this? You might be better served using String.toCharArray().

I know one will return you an array of Strings while the other will give you an array of chars. Since you want each character separately, I am assuming this doesn't matter to your code.

Sunday, December 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :