LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-20-2021, 12:06 PM   #1
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Rep: Reputation: Disabled
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
 
Old 12-20-2021, 12:47 PM   #2
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-21-2021, 12:26 PM   #3
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply

Tags
extracts, in_array, php, substring



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 08:36 AM
Urgent PHP problem with "undefined function: domxml_open_mem()"! Recomplie php? Oskare100 Linux - Server 0 12-27-2006 12:28 PM
PHP / VideoLAN / Fedora Core Question - how can I get the PHP "exec" function to work gtrawoger Linux - Software 3 12-21-2005 06:51 AM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration