Explode array in a for loop

	$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?

Just a curiousity: what are you doing in these loops? Isn’t there a $i missing somewhere?


    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]));
    }

Btw, why don’t you use a foreach to loop through the arrays?

Now to your question: if you want to make $heb an array, try


$heb[] = explode(")", $file_2[1][$i]);

	$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(" ", "", 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);
	$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 “)”.

But the two loops I mentioned do the str_replace operation each time to the entire array, not only to the element you’re looping through. Are you sure the first loop shouldn’t be


for($i=0; $i<count($file_1[4]); $i++){
    $href=str_replace(" ", "", str_replace("\\"/", "", $file_1[4][B][COLOR="Red"][SIZE="3"][$i][/SIZE][/COLOR][/B]));
}

And I see you modified the last loop. Does $heb_title contain what you wanted?

they’re fine.

	$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("\	", "", 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 “)”.

Array
(
[0] => http://www.sbl-site.org/Resources/Resources_BiblicalFonts.aspx
[1] => http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&item_id=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.