Tuesday 7 January 2020

php - file_get_contents shows unexpected output while reading a file



I want to output an inline jpg image as a base64 encoded string, however when I do this :




$contents = file_get_contents($filename);
print "";


Where $filename is a local text file with the base64 image. The output is as follows :






And obiously the image is not rendered, but where does  come from ? It is not in the text file. If removed, the image is displayed properly.



Answer



It's a Unicode Byte-Order Mark. The file was saved with an editor that added the BOM to indicate the file is encoded as UTF-8. So those bytes actually are in the file, but a text editor just won't show it since it's not text. For storing this kind of data you'll want to remove the BOM. Easiest way would be to configure your editor not to add the BOM, but if you don't have influence over the creation process of the file you could to it on-the-fly in your script too:



print "";

java.util.scanner - Last line of input not scanning in Java Scanner Class



I am trying to work on some stuff using the Scanner for input, but for some reason, the scanner is failing me.



I have the following code running, nothing complex:




while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
}


Just as a test bed to make sure everything is being input correctly. If I copy my test material, say



5

4
3
2
1


The output omits the last line. I am pretty sure that it is because nextLine will only return a string if there is a string after it, even if it is empty, but I need it to work this way for the way I will eventually input the data to work. Does anyone know how I can fix this? My only guesses were to either use something other that scanner or to somehow append an empty string to the end of the input, but I wasn't sure how to do the second one, and I didn't think a different scanner-like thing would work. Thanks in advance!


Answer



Your last line MIGHT not have a "enter" char. So it does not treat it as the end of line. Try




> 5
> 4
> 3
> 2
> 1
>

How to implement Random.nextDouble() in javascript?

I am using Math.floor((Math.random() * max-min)+min) for random number generation,
what is the difference between this function and random.nextDouble() ?
How to implement random.nextDouble in Javascript?

What's the strangest corner case you've seen in C# or .NET?





I collect a few corner cases and brain teasers and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:



string x = new string(new char[0]);
string y = new string(new char[0]);
Console.WriteLine(object.ReferenceEquals(x, y));



I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should. Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)



Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.



Any other gems lurking out there?


Answer



I think I showed you this one before, but I like the fun here - this took some debugging to track down! (the original code was obviously more complex and subtle...)




    static void Foo() where T : new()
{
T t = new T();
Console.WriteLine(t.ToString()); // works fine
Console.WriteLine(t.GetHashCode()); // works fine
Console.WriteLine(t.Equals(t)); // works fine

// so it looks like an object and smells like an object...

// but this throws a NullReferenceException...

Console.WriteLine(t.GetType());
}


So what was T...



Answer: any Nullable - such as int?. All the methods are overridden, except GetType() which can't be; so it is cast (boxed) to object (and hence to null) to call object.GetType()... which calls on null ;-p







Update: the plot thickens... Ayende Rahien threw down a similar challenge on his blog, but with a where T : class, new():



private static void Main() {
CanThisHappen();
}

public static void CanThisHappen() where T : class, new() {
var instance = new T(); // new() on a ref-type; should be non-null, then
Debug.Assert(instance != null, "How did we break the CLR?");
}



But it can be defeated! Using the same indirection used by things like remoting; warning - the following is pure evil:



class MyFunnyProxyAttribute : ProxyAttribute {
public override MarshalByRefObject CreateInstance(Type serverType) {
return null;
}
}
[MyFunnyProxy]

class MyFunnyType : ContextBoundObject { }


With this in place, the new() call is redirected to the proxy (MyFunnyProxyAttribute), which returns null. Now go and wash your eyes!


android - How to resolve Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)?

I have an app in which i am getting error "Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)" please explain meaning and solution for that



code:-



 String receivedResult = parseJsonResultSetFav(result);
if (receivedResult.equalsIgnoreCase("SUCCESS")) {
CustomerTicketDialogClass ctdc = new CustomerTicketDialogClass(getActivity(),
"successful", "successfully sent mail to your email id ", "ViewDetails");
ctdc.show();

ctdc.setCanceledOnTouchOutside(false);
} else {
CustomerTicketDialogClass ctdc = new CustomerTicketDialogClass(getActivity(),
"failure", receivedResult, "ViewDetails");
ctdc.show();
ctdc.setCanceledOnTouchOutside(false);
}

unicode - What characters can be used for up/down triangle (arrow without stem) for display in HTML?



I'm looking for a HTML or ASCII character which is a triangle pointing up or down so that I can use it as a toggle switch.



I found ↑ (), and ↓ () - but those have a narrow stem. I'm looking just for the HTML arrow "head".


Answer



Unicode arrows heads:




  • ▲ - U+25B2 BLACK UP-POINTING TRIANGLE


  • ▼ - U+25BC BLACK DOWN-POINTING TRIANGLE

  • ▴ - U+25B4 SMALL BLACK UP-POINTING TRIANGLE

  • ▾ - U+25BE SMALL BLACK DOWN-POINTING TRIANGLE



For ▲ and ▼ use and respectively if you cannot include Unicode characters directly (use UTF-8!).



Note that the font support for the smaller versions is not as good. Better to use the large versions in smaller font.



More Unicode arrows are at:






Lastly, these arrows are not ASCII, including ↑ and ↓: they are Unicode.


javascript - Check if a variable is of function type



Suppose I have any variable, which is defined as follows:




var a = function() {/* Statements */};


I want a function which checks if the type of the variable is function-like. i.e. :



function foo(v) {if (v is function type?) {/* do something */}};
foo(a);


How can I check if the variable a is of type Function in the way defined above?



Answer



Sure underscore's way is more efficient, but the best way to check, when efficiency isn't an issue, is written on underscore's page linked by @Paul Rosania.



Inspired by underscore, the final isFunction function is as follows:



function isFunction(functionToCheck) {
return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...