explode array in a for loop
PHP Code:
$pattern=Array();
$pattern[0] = "<a(\s+(title=(\"[^\"]*\")|href=(\"[^\"]*\")|\w+=(\"[^\"]*\"|\S+)))+>";
$pattern[1] = "#<a.*>(.+)</a#Ui"; //preg_match pattern in a for loop to get the href, title and innerhtml and exclude the book title in brackets
preg_match_all($pattern[0], $file, $file_1);
for($i=0; $i<count($file_1[4]); $i++){
$href=str_replace("\"", "", $file_1[4]);
}
for($i=0; $i<count($file_1[3]); $i++){
$title=str_replace(" ", "", str_replace("\"", "", $file_1[3]));
}
preg_match_all($pattern[1], $file, $file_2);
print_r($file_2[1]);
for($i=0; $i<count($file_2[1]); $i++){
$heb=explode(")", $file_2[1][$i]);
}
//var_dump($heb);
I realize that I don't need to declare $heb as an array $heb=Array(); but I need to gather $file_2[1][$i][1] into one array. How would that be possible?
Re: explode array in a for loop
first of all, what is that variable supposed to even contain?
Re: explode array in a for loop
Quote:
Originally Posted by
kows
first of all, what is that variable supposed to even contain?
PHP Code:
$file_link = "http://wlc.hebrewtanakh.com/sidemenu.htm";
$file = file_get_contents($file_link);
$pattern=Array();
$pattern[0] = "<a(\s+(title=(\"[^\"]*\")|href=(\"[^\"]*\")|\w+=(\"[^\"]*\"|\S+)))+>";
$pattern[1] = "#<a.*>(.+)</a#Ui"; //preg_match pattern in a for loop to get the href, title and innerhtml and exclude the book title in brackets
preg_match_all($pattern[0], $file, $file_1);
for($i=0; $i<count($file_1[4]); $i++){
$href=str_replace("\t", "", str_replace("\"", "", str_replace("\"/", "", $file_1[4])));
}
print_r($href);
for($i=0; $i<count($file_1[3]); $i++){
$title=str_replace(" ", "", str_replace("\"", "", $file_1[3]));
}
print_r($title);
preg_match_all($pattern[1], $file, $file_2);
$heb=Array();
for($i=0; $i<count($file_2[1]); $i++){
$heb=explode(")", $file_2[1][$i]);
$heb_title[]=$heb[1];
}
print_r($heb_title);
the url shows a menu of links. So I'm taking the links and putting it in arrays of href, title and innerhtml. The innerhtml has to be split where there is a ")".
Quote:
Array
(
[0] =>
http://www.sbl-site.org/Resources/Re...icalFonts.aspx
[1] =>
http://scripts.sil.org/cms/scripts/p...d=SILHebrUnic2
[2] => genesis/1.htm
[3] => exodus/1.htm
[4] => leviticus/1.htm
[5] => numbers/1.htm
[6] => deuteronomy/1.htm
[7] => joshua/1.htm
[8] => judges/1.htm
[9] => ruth/1.htm
[10] => 1_samuel/1.htm
[11] => 2_samuel/1.htm
[12] => 1_kings/1.htm
[13] => 2_kings/1.htm
[14] => 1_chronicles/1.htm
[15] => 2_chronicles/1.htm
[16] => ezra/1.htm
[17] => nehemiah/1.htm
[18] => esther/1.htm
[19] => job/1.htm
[20] => psalms/1.htm
[21] => proverbs/1.htm
[22] => ecclesiastes/1.htm
[23] => songs/1.htm
[24] => isaiah/1.htm
[25] => jeremiah/1.htm
[26] => lamentations/1.htm
[27] => ezekiel/1.htm
[28] => daniel/1.htm
[29] => hosea/1.htm
[30] => joel/1.htm
[31] => amos/1.htm
[32] => obadiah/1.htm
[33] => jonah/1.htm
[34] => micah/1.htm
[35] => nahum/1.htm
[36] => habakkuk/1.htm
[37] => zephaniah/1.htm
[38] => haggai/1.htm
[39] => zechariah/1.htm
[40] => malachi/1.htm
)
Array
(
[0] =>
[1] =>
[2] => Genesis
[3] => Exodus
[4] => Leviticus
[5] => Numbers
[6] => Deuteronomy
[7] => Joshua
[8] => Judges
[9] => Ruth
[10] => 1Samuel
[11] => 2Samuel
[12] => 1Kings
[13] => 2Kings
[14] => 1Chronicles
[15] => 2Chronicles
[16] => Ezra
[17] => Nehemiah
[18] => Esther
[19] => Job
[20] => Psalms
[21] => Proverbs
[22] => Ecclesiastes
[23] => SongofSolomon
[24] => Isaiah
[25] => Jeremiah
[26] => Lamentations
[27] => Ezekiel
[28] => Daniel
[29] => Hosea
[30] => Joel
[31] => Amos
[32] => Obadiah
[33] => Jonah
[34] => Micah
[35] => Nahum
[36] => Habakkuk
[37] => Zephaniah
[38] => Haggai
[39] => Zechariah
[40] => Malachi
)
Array
(
[0] =>
[1] => בראשית
[2] => שמות
[3] => ויקרא
[4] => במדבר
[5] => דברים
[6] => יהושע
[7] => שופטים
[8] => רות
[9] => שמואל א
[10] => שמואל ב
[11] => מלכים א
[12] => מלכים ב
[13] => דברי הימים א
[14] => דברי הימים ב
[15] => עזרא
[16] => נחמיה
[17] => אסתר
[18] => איוב
[19] => תהילים
[20] => משלי
[21] => קהלת
[22] => שיר השירים
[23] => ישעה
[24] => ירמיה
[25] => איכה
[26] => יחזקאל
[27] => דניאל
[28] => הושע
[29] => יואל
[30] => עמוס
[31] => עבדיה
[32] => יונה
[33] => מיכה
[34] => נחום
[35] => חבקוק
[36] => צפניה
[37] => חגי
[38] => זכריה
[39] => מלאכי
)
So the first two results from the first 2 arrays have to be skipped. The 3rd array, only the 1st needs to be skipped.
Re: explode array in a for loop
I really don't understand what you're trying to do. there are no parenthesis in any of the values you posted, so your explode() does nothing. I'm not even sure what your explode() would accomplish anyway. try explaining your issue in more detail.
if you just need to skip something if the value is empty, just use the continue statement:
PHP Code:
for($i = 0; $i < 6; i++){
if($i == 0){
continue;
}
echo $i;
}
this loop will skip 0 when printing the numbers in the loop.
Re: explode array in a for loop
how do you exclude something out from the ()?
(xxxxxxxxx)yyyyyyyyy
I only want
yyyyyyyyy
Re: explode array in a for loop
the easiest way to split that up would be a regular expression. something like this should work:
PHP Code:
$expr = '/\(.*?\)(.*?)/i';
preg_match_all($expr, $string, $matches);
print_r($matches);
you could also find the position of the last end parenthesis using strrpos() and then use substr() to create a string using that position as a starting point.