PDA

View Full Version : Explode yeilds fatal error


SilverVenom
12-12-2007, 11:55 PM
Hey, I am working on a very easy script that breaks apart a sentence and am running into some seemingly very simple problems. I have been working with Java for the past 6 months non stop, so I've forgotten quite a lot of what I used to know about PHP. :rolleyes:

So here is my problem:

This simple code should break apart the sentence into words, which are put into the array $split. Each word is checked in this array to see if it is equal to "be". If it is it makes the word bold faced and adds it to $checked, otherwise just adds it with out the bold face, then echo the output.

When I run this code it says "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2359241 bytes) in ...docFix.php on line 11"

Without the for() loop it works fine, but as soon as I add the for loop it breaks. What it wrong? It couldn't possibly be using too much memory for something so simple. :eek:


<?php
$input = "To be or not to be, that is the question!";

$split = explode(" ", $input);
$checked = "";

for($i=0;$i<=count($split);$i++) {
if($split[$i] = "be") {
$checked .= "<b>".$split[$i]."</b> ";
} else {
$checked .= $split[$i]." ";
}
}

echo($checked);

?>


Thanks in advance for the help! - Peter

CyanBlue
12-13-2007, 01:11 PM
Your if statement requires '==' not '='... ;)\
<?php

$input = "To be or not to be, that is the question!";

$split = explode(" ", $input);
$checked = "";

for($i=0;$i<=count($split);$i++) {
if ($split[$i] == "be")
{
$checked .= "<b>".$split[$i]."</b> ";
}
else
{
$checked .= $split[$i]." ";
}
}

echo($checked);

?>