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.
Related
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
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
Why when it displays does it say Resource id #2?
All I am doing is fetching a simple number (0) out of a .txt file:
$num = fopen('qnum/qnum.txt', 'r');
echo $num;
You are not actually reading the file. You are only opening it and acquiring a reference to the file. You have to add more code (e.g. something along the lines of the answers to this question) to read the contents of the file.
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;
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
Hi I want to insert data from text are into database but what I want is every line of text are should occupy a new row in the database. e.g
number one
number two
number three
this is the content of the text area and should be inserted into three different rows of the table.
Use PHP's explode() to split the textarea's content up at each line break. So something like:
$lines = explode("\n", $textarea_content);
$lines will then be an array.
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 am pulling out data from a table, but instead of breaking a line after each result(or set of results), I would like to break the line after 'x' amount of those results, so I don't get long pages.
How can this be done with PHP?
You haven't posted any code so your question is going to get marked as off-topic.
If you're trying to do line breaks every X number of iterations then just do it.
$i = 0;
while ($row1 = mysql_fetch_assoc($result1))
{
if ($i % 5==0) {echo '<div>Multiple of 5, line break here</div>';}
$i++;
}
You have to use a pagination. Use something like this http://stefangabos.ro/php-libraries/zebra-pagination/