Jump to content

Archived

This topic is now archived and is closed to further replies.

marcus

explode() help needed

Recommended Posts

Ok, I'm having trouble exploding text from my file facts.txt

[code]
<?
$r = rand(1,1);
$f = explode("*",facts.txt);
echo $f[$r];
?>
[/code]

what am i doing wrong?

Share this post


Link to post
Share on other sites
You have to read the file first. What are you trying to do?

Ken

Share this post


Link to post
Share on other sites
I am trying to explode text from the file.

Would I have to do

[code]
<?
$file = fopen("facts.txt","r");
$f = explode("*",$file);
$r = rand(1,1);
echo $f[$r];
?>
[/code]

Share this post


Link to post
Share on other sites
Explode() takes a string as it's second parameter. In your script, the variable $file is a file handle not a string. From the description of what you want to do, it looks like you file contains works seperated by the "*" character and you want to select one at random.

Try this:
[code]<?php
$fc = file_get_contents('facts.txt'); //read the contents of the file into a string
$fc_array = explode('*',$fc); // create the array
echo $fc_array[array_rand($fc_array)]; // use the function array_rand() to get a random index in the $fc_array array.
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.