LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   PHP IN_Array function (https://www.linuxquestions.org/questions/linux-software-2/php-in_array-function-4175705191/)

TBotNik 12-20-2021 12:06 PM

PHP IN_Array function
 
All,

Working on a script to convert online search results download files into a MySQL .sql input file. Code is below.

I have no problem scanning the directory with the files and the 1st foreach loop eliminates all non .html files and .html~ edit files.

The "$filr = file($s_path.$fval);" statement correctly loads the file into an array for processing, showing the contents correctly displayed by the print_r statement.

What is not working are the "in_array" statements so only the "get_flat" function is being called. The 3 different files downloaded are either table, div or flat-file display source.

The 3 different functions exist, because"
  1. The "Table" processing looks for and extracts rows,
  2. The DIV looks for DIV class to extract rows,
  3. The FLAT uses each non-blank line to extract
    • Name,
    • URL,
    • City,
    • State.
    But there are variable blanks in the lines of data and not alignable by column location.

First time for me using in_array and not sure if it is looking for an entire element such as:

Code:

$ray[1] = 'table'
Instead of finding 'table' as a substring in the array element.
Code:

        $f_ray        =        scandir ( $s_path );
        foreach ($f_ray as $fval) {
                // Only read the .html files
                $posstr        =        strpos ($fval, 'html');
                $edtfil        =        strpos ($fval, '~');
                if ( ( $posstr < 1 ) || ( $edtfil > 1 ) ) { continue; }
                $fil_r[]        =        $fval;
        }        // end foreach $f_ray       
        $o_ray        =        array();
        foreach ($fil_r as $fval) {
                echo "File => $fval \n";
                $filr = file($s_path.$fval);
                print_r ( $filr );                // Works corretly
                if ( in_array ( '<table', $filr ) )        { get_table ( $fval, $filr ); }
//                if ( in_array ( '<div', $filr ) )                { get_div ( $fval, $filr ); }
                get_flat ( $fval, $filr );        // Only function being called
        }        // end foreach $fil_r       
exit;

Hope someone can explain how "in_array" actually works and if it will either need "htmlspecialcharacters" or another approach to find the substrings in the array I'm looking for!

Cheers!

TBNK

TBotNik 12-20-2021 12:47 PM

All,

According to:

https://stackoverflow.com/questions/...l-string-match

There has to be this function:

Code:

function array_search_partial($arr, $keyword) {
    foreach($arr as $index => $string) {
        if (strpos($string, $keyword) !== FALSE)
            return $index;
    }
}

I added it as:

Code:

function fnd_raysubstr ( $myval, $myray ) {
        echo "Finding Substr in Array!";
        foreach($myray as $index => $str) {
                if (strpos($str, $myval) !== FALSE)
                        return $index;
                }        // end if strpos
        }        // end foreach $myray
}        // end function fnd_raysubstr

Tesing it now!

Cheers!

TBNK

TBotNik 12-21-2021 12:26 PM

All,

That function worked just fine. PHP should have a command/function that actually does this natively. Where do I post it as a suggestion?

Cheers!

TBNK


All times are GMT -5. The time now is 02:53 AM.