PHP Help
by TBotNik from LinuxQuestions.org on (#55E4S)
All,
OK, I have this PHP cli script to scrub tags from all .html files in a directory. If all the tags being sought were in one line of the file, then I think my current code would work, but usually the code is on different lines, so I cannot assign the array:
Code: $array[] = ('num'=>$arraylinenumber,'nam'=>$r_nam,'add'=>$r_add,'cty'=>$r_cty,'stt'=>$r_stt,'zip'=$r_zip,'phn'=$r_phn,'abt'>$r_abt);until I know all the elements are non-blank and then clear them after the array assignment. So I need to put the array assignment outside the "foreach lines" loop, but having issues about how best to record the non-blank array element to preserve them for the array line write.
I'm still thinking about the best way to do this and here is my current code is:
Code: $src_lst = scandir ( "$src_dir" );
foreach ( $src_lst as &$file ) {
$sps = strpos ( $file, ".html");
if ( $sps == false ) { continue; }
$ret_ray[] = get_info ( $file );
} // end foreach $src_list
function get_info ( $inf ) {
$lines = file ( $inf );
$o_ray = array();
$ray_cnt = 0;
foreach ( $lines as $l_num => $line ) {
$r_nam = get_name ( $line );
$r_add = get_street ( $line );
$r_cty = get_city ( $line );
$r_stt = get_state ( $line );
$r_zip = get_zip ( $line );
$r_phn = get_phone ( $line );
$r_abt = get_about ( $line );
$o_ray[]="'nam'=>$r_nam,'add'=>$r_add,'cty'=>$r_cty,
'stt'=>$r_stt,'zip'=$r_zip,'phn'=$r_phn,
'abt'=>$r_abt";
} // end foreach $lines
} // end function get_info
function get_name ( $lin ) {
if ( strpos ( $line, 'itemprop="url">') == true ) {
$nps = strpos ( $line, 'itemprop="url">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</a>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_street ( $lin ) {
if ( strpos ( $line, 'streetAddress">') == true ) {
$nps = strpos ( $line, 'streetAddress">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_city ( $lin ) {
if ( strpos ( $line, 'addressLocality">') == true ) {
$nps = strpos ( $line, 'addressLocality">') + 17;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_state ( $lin ) {
if ( strpos ( $line, 'addressRegion">') == true ) {
$nps = strpos ( $line, 'addressRegion">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_zip ( $lin ) {
if ( strpos ( $line, 'postalCode">') == true ) {
$nps = strpos ( $line, 'postalCode">') + 12;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_phone ( $lin ) {
if ( strpos ( $line, 'telephone">') == true ) {
$nps = strpos ( $line, 'telephone">') + 11;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_about ( $lin ) {
if ( strpos ( $line, 'itemprop="about">') == true ) {
$nps = strpos ( $line, 'itemprop="about">') + 17;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</p>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_infoOh. the final goal is to build the "INSERT VALUE" rows into a .sql file for import into MySQL!
Open to your suggestions here!
Cheers!
TBNK


OK, I have this PHP cli script to scrub tags from all .html files in a directory. If all the tags being sought were in one line of the file, then I think my current code would work, but usually the code is on different lines, so I cannot assign the array:
Code: $array[] = ('num'=>$arraylinenumber,'nam'=>$r_nam,'add'=>$r_add,'cty'=>$r_cty,'stt'=>$r_stt,'zip'=$r_zip,'phn'=$r_phn,'abt'>$r_abt);until I know all the elements are non-blank and then clear them after the array assignment. So I need to put the array assignment outside the "foreach lines" loop, but having issues about how best to record the non-blank array element to preserve them for the array line write.
I'm still thinking about the best way to do this and here is my current code is:
Code: $src_lst = scandir ( "$src_dir" );
foreach ( $src_lst as &$file ) {
$sps = strpos ( $file, ".html");
if ( $sps == false ) { continue; }
$ret_ray[] = get_info ( $file );
} // end foreach $src_list
function get_info ( $inf ) {
$lines = file ( $inf );
$o_ray = array();
$ray_cnt = 0;
foreach ( $lines as $l_num => $line ) {
$r_nam = get_name ( $line );
$r_add = get_street ( $line );
$r_cty = get_city ( $line );
$r_stt = get_state ( $line );
$r_zip = get_zip ( $line );
$r_phn = get_phone ( $line );
$r_abt = get_about ( $line );
$o_ray[]="'nam'=>$r_nam,'add'=>$r_add,'cty'=>$r_cty,
'stt'=>$r_stt,'zip'=$r_zip,'phn'=$r_phn,
'abt'=>$r_abt";
} // end foreach $lines
} // end function get_info
function get_name ( $lin ) {
if ( strpos ( $line, 'itemprop="url">') == true ) {
$nps = strpos ( $line, 'itemprop="url">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</a>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_street ( $lin ) {
if ( strpos ( $line, 'streetAddress">') == true ) {
$nps = strpos ( $line, 'streetAddress">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_city ( $lin ) {
if ( strpos ( $line, 'addressLocality">') == true ) {
$nps = strpos ( $line, 'addressLocality">') + 17;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_state ( $lin ) {
if ( strpos ( $line, 'addressRegion">') == true ) {
$nps = strpos ( $line, 'addressRegion">') + 15;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_zip ( $lin ) {
if ( strpos ( $line, 'postalCode">') == true ) {
$nps = strpos ( $line, 'postalCode">') + 12;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_phone ( $lin ) {
if ( strpos ( $line, 'telephone">') == true ) {
$nps = strpos ( $line, 'telephone">') + 11;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</span>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_info
function get_about ( $lin ) {
if ( strpos ( $line, 'itemprop="about">') == true ) {
$nps = strpos ( $line, 'itemprop="about">') + 17;
$nln = substr ( $line, $nps );
$eps = strpos ( $nln, '</p>');
$nam = substr ( $nln, 0, $eps );
return $nam
} // end if strpos
} // end function get_infoOh. the final goal is to build the "INSERT VALUE" rows into a .sql file for import into MySQL!
Open to your suggestions here!
Cheers!
TBNK