Jump to content

A Complicated Empty delimiter Issue


soma56

Recommended Posts

Complicated for me anyways. The script I'm working on works fine. When 'My list' check-box is ticked and there are values in the textarea then it scrubs fine against my 'check list'.

 

<?PHP
   
//Asign variables 
$notselected = "Not Selected";
$selected = "Selected";

if (isset($_POST['Submit'])) {

//A general purpose list
if (isset($_POST['mylistcheckbox'])) {
$mylist = explode("\n", $_POST['mylist']); 
$mylistcheckbox = $selected;
} else { 
$mylist = $notselected;
}

//A list to srub against for duplicates
if (isset($_POST['listcheckbox'])){
$checklist = explode("\n", $_POST['checklist']);
$listcheckbox = $selected;
} else {
checklist = $notselected;
}

//Strpos function
function strpos_array($haystack, $search){

global $$mylist;

    foreach($search as $term){
        if(stristr($haystack, $term) !== false){
            return true;
        }
    }
    return false;
}

//if statement for $mylist
if (($mylistcheckbox == $selected) && ($checklist != 0)){
foreach($mylist as $key => $val){
    if (strpos_array($val, $checklist)){
        unset ($mylist[$key]);
    }
} 
} 

//end isset

}

?>

 

However, I'm having issues when one of two issues occurs:

 

When the checkbox is ticked on the previous form (and the corresponding textarea is blank) the script still wants to process the above function. I receive a "Warning: stristr() [function.stristr]: Empty delimiter in..." I've tried 'less then', 'empty', 'Not equal to' but it still wants to process resulting in this error.

 

Further more should I hit the enter button a few times after typing a line in my 'checklist' I receive the same error. I did some research and discovered this:

$checklist = str_replace("\0", "", $checklist);

but alas it is not working.

 

Suggestions are always appreciated.

Link to comment
https://forums.phpfreaks.com/topic/207079-a-complicated-empty-delimiter-issue/
Share on other sites

After searching for a solution for the past few hours I'm concluding that it's centered around something to do with the extra blank line being received in the form.

 

This function will not work if I bring it in an array that has a new line - if someone hits 'enter' in the textarea before hitting submit:

 

function strpos_array($haystack, $search){

    foreach($search as $term){
        if(stristr($haystack, $term) !== false){
            return true;
        }
    }
    return false;
}

foreach($mylist as $key => $val){
    if (strpos_array($val, $myarray)){
        unset ($mylist[$key]);
    }

 

I tried remove the line by checking the length:

function validElement($element) {
    return strlen($element) > 1;
}

$filtered_array = array_values(array_filter($myarray, "validElement"));

 

This looked promising:

function replace_newline($string) {
  return (string)str_replace(array("\r", "\r\n", "\n"), '', $string);
}

 

I've also tried adding a new line after each variable thinking maybe the function needed a "\n"

$newlist = array_values($filtered_array); 
foreach ($newlist as $key => $val){
$val = "$val"."\n";
echo $val;
}

 

Oddly, the script works fine if the textarea is submitted as:

 

Apples
Oranges
Peaches

 

But returns nothing when brought in like this:

 

Apples
Oranges
Peaches



 

wtf?

I've discovered that the script is comparing against the last instance of where the 'enter' key is in the textarea prior to pushing the submit button. Here's a complete copy of the script:

 

<form action="" method="post">
Filter Array Input<textarea name="filter">
</textarea>
<br><br>
Compare Against Array Input<textarea name="compare" ></textarea>
<br><br>
<input type="submit" name="Submit" value="Start">
</form>



<?PHP
   

if (isset($_POST['Submit'])) {

$filter = explode("\n", $_POST['filter']); 


$compare = explode("\n", $_POST['compare']);  

function validElement($element) {
    return strlen($element) > 1;
}

$filtered_array = array_values(array_filter($filter, "validElement"));

function filterArrayByWords($inputAry, $excludeWordsAry, $returnInclude=true)
{
    $excludeAry = array();
    $includeAry = $inputAry;
    foreach($includeAry as $keyInt => $valueStr)
    {
        foreach($excludeWordsAry as $excludeWordStr)
        {
            //Check for exclude word using word boundrys
            if(preg_match("/\b{$excludeWordStr}\b/i", $valueStr)!=0)
            {
                $excludeAry[$keyInt] = $valueStr;
                unset($includeAry[$keyInt]);
                break;
            }
        }
    }
    return ($returnInclude) ? $includeAry : $excludeAry;
}

print_r(filterArrayByWords($compare, $filtered_array));
echo "<br /><br />"; 
print_r(filterArrayByWords($compare, $filtered_array, false));


//end isset
}

?>

 

Try placing this in the bottom box:

good
bad
ugly

 

and one of the same to compare against in the top box. Clicking submit should result in:

Array ( [1] => bad [2] => ugly )

Array ( [0] => good ) 

 

Now try it again and hit enter a few times after placing a word in the top box. The results come back:

Array ( [0] => good [1] => bad [2] => ugly )

Array ( ) 

 

And as a result the filter doesn't work. There must be some explanation or easy fix.

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.