comparision not working for file_get_content value [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file with content
1
2
3
<?php
$input=file_get_contents('/home/abhishek/Desktop/input');
echo $input ;// i get 1 2 3 as output
$x='1 2 3';
if(trim($input)==trim($x))
echo "hellllliff--";
else
echo "helllllelse";
?>
why it goes to else????

The new line characters need to be removed - trim would only trim whitespace, not the \n \r characters.
preg_replace("/[\n\r]/","",$input);
Would get rid of them if that's the intention. Or alter your comparison string to have new lines too (although watch out for the unix versus pc variance in the new line character)

1
2
3 in three lines is not equals to
1 2 3 in one line
so it's get false, when compare.For further explaination,$input = 1/n2/n3

Related

Remove characters from a string till another character comes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I remove characters till there comes another character in a string?
For example:
String is this: 0030051
Now I want to remove ALL 0's before another character (like: 1) comes.
So the string will become this: 30051.
Is there a PHP function for this or is this easier with Javascript/jQuery ? (I'd prefer PHP)
cast it to integer
$var = (int)$var;
Source - Reference

How can I separate hour and minute digits from a date string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have, for example the string 2013-12-08 19:55:19 and from this string I want to obtain 19:55. How can I do this in PHP?
You can go parsing the datetime to an object and stuff, or you can simply do:
substr($myString,11,5)
(take 5 characters starting from the 11th character)
Only condition is that your string always has 4 digits for the year. And 2 digits for all other elements. Just like in your OP
Check out strtotime() and date():
echo date('G:i', strtotime('2013-12-08 19:55:19'));
You can use regex:
$str = "2013-12-08 19:55:19";
preg_match("/.*(\d\d\:\d\d)\:\d\d\.*/", $str, $arr);
echo "time: " . $arr[1];
OUTPUT
time: 19:55

php query where a word like and skip before words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
php script query out put is
"some word STAR some other words", I want remove all workd before "Star". I need all other words after "Star".
I using php script output from a mysql file
$newStr = substr_replace($str, '', 0, strpos($str,"STAR"));
Where $str is a variable with a string, eg: "some word STAR some other words"
$arrayStr = explode('STAR', 'some word STAR some other words');
echo $arrayStr[1];
DEMO
Some documentation:
php.net explode() function

How to echo random line from text file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need to echo random lines from a text file like so
textfile.txt
line 1
line 2
line 3
line 4
line 5
line 6
index.php
line 4
im not great at php so any help would be greatly appreciated
You can do this quite easily using the file and array_rand functions:
$lines = file($filename, FILE_IGNORE_NEW_LINES);
echo $lines[array_rand($lines)];
file returns an array of all the lines of the file and stores it in $lines.
array_rand picks a random index from the $lines array and the item at that index is echoed.

Find missing numbers in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an php array of 99 numbers.
The array contains the digits 1 to 100 with one digit missing.
How to know the missing number?
Since you say you've found an answer yourself, here is a possible solution
$assignmentArray = array(1,2,3,4,5,6,7,8,10);
// find the missing fie... err.. missing value is actually more correct.
$missingNumber = array_sum(range(1, 10)) - array_sum($assignmentArray);
echo 'The missing number is: ' . $missingNumber;

Categories