Jump to content

[SOLVED] Str_Relace not working as needed


Grimloch

Recommended Posts

Hi everyone. Ol' Grimlochs' at it again. I can't seem to get the results I need trying to use str_replace function. This is a random quote script I'm working on. Right now it will display ALL of the quotes in the text file; later I will add the random seed code to pick one only to display. In the meantime I need to get rid of the 'delimiter characters' between array string elements. The delimiter characters are:

\n%%\n = newline, %, %, newline.

 

This is an example of the text file:

 

A long line or two of text.

%%

Another long line or two of text.

%%

etc. etc. etc.

 

In the code below I have added the str_replace code to try to get rid of the delimiter stuff. It doesn't work. I'm probably doing something very dumb!!

 

$handle = @fopen("http://COMPLETE_URL_PATH/data/random1.txt", "rb");
if ($handle) {
    while (!feof($handle)) {
        $quote = fgets($handle);
$array = explode("\n%%\n", $quote);
reset($array);
foreach($array AS $daily) {
$br = '<br /><br />';
$new_daily = str_replace("\n%%\n", $br, $daily);
        echo $new_daily ."<br>";
}
    }
    fclose($handle);
}

 

Any guidance would be appreciated and yes; I have read the HP Manual on str_replace and thought I could make it work right. Thanks in advance.

 

:facewall:

Link to comment
https://forums.phpfreaks.com/topic/168647-solved-str_relace-not-working-as-needed/
Share on other sites

OK; after hitting Unsolved, I still can't edit the first post so; this is a continuation of it.

 

I did solve the str_replace problem I had. Now I have another problem but it's related. In the following code the script displays all 68 quotes to the screen. I need help figuring out how to pull only ONE quote from the array at random to display. I have tried several different ways to get it to work but I've never used rand in php before and am not able to get my head around it. I sure would appreciate any help I can get Here is the code:

 

    echo "<td>\n";

$handle = @fopen("http://FULL.PATH.TO.FILE/data/random1.txt", "rb");
if ($handle) {
    while (!feof($handle)) {
        $quote = fgets($handle);
$array = explode("\n%%\n", $quote);
reset($array);
foreach($array AS $daily) {
$br = ' ';
$new_daily = str_replace("%%", $br, $daily);

    echo $new_daily."<br />";
}
    }
    fclose($handle);
}
    echo "</td>\n";

First of all you need to be clear about rand() method for which you can have a look at:

http://www.php.net/manual/en/function.rand.php.

 

You can use rand(min,max)  where min= minimum value of the range and max= maximum value of the range where the random value should lie.

Ex: rand(1,10) should return the value between 1-10, but it is not always sure that for every consecutive attempt you get the unique values.

OK well, here are the final results.

 

@phpSensei: Your idea gave me nothing but an array of numbers, but it gave me some ideas.

 

I guess I am slowly learning how to do my own stuff instead of just manipulating code that other people have created. PHP is a long road to travel; wish I had started years aga instead of only several months. Anyway I finally have the complete result exactly as I wanted it. Here is the final code that works flawlessly. I'll also include a sample of the actual data file:

 

    echo "<td>\n";
$handle = @fopen("http://www.MYDOMAIN/infusions/random_quotes_panel/data/data1.txt", "rb");
$quotes = stream_get_contents($handle);
fclose($handle);
$new_quote = split("%%", $quotes);
shuffle($new_quote);
    echo $new_quote[0];
    echo "</td>\n";

 

And here is a small sample of the actual data file:

 

Maintenance Request: Left inside main tire almost needs replacement.<br>
Mechanic: Almost replaced left inside main tire.
%%
Maintenance Request: Test flight OK, except auto-land very rough.<br>
Mechanic: Auto-land not installed on this aircraft.
%%
Maintenance Request: Something loose in cockpit.<br>
Mechanic: Something tightened in cockpit.
%%
Maintenance Request: Dead bugs on windshield.<br>
Mechanic: Live bugs on back-order.
%%
Maintenance Request: Evidence of leak on right main landing gear.<br>
Mechanic: Evidence removed.
%%
Maintenance Request: DME volume unbelievably loud.<br>
Mechanic: DME volume set to more believable level.

 

Thanks everyone for your help. I'll mark this as solved!

Archived

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

×
×
  • Create New...

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.