Jump to content

Rules that Change! Putting In Too Array.


paulman888888

Recommended Posts

hi;

 

I am trying to create a PHP script that can read come script.

 

Below is the code i am trying to convert to some PHP can understand

L[0] = new Array('','','','W','','','','','','','','','','','','','','T','');// The length of the row and letters WILL change
L[1] = new Array('S','E','C','O','N','D','A','R','Y','','R','E','T','R','I','E','V','A','L');// The length of the row and letters WILL change
L[2] = new Array('','','','R','','','','','','','','','','','','','','P','');// The length of the row and letters WILL change
L[3] = new Array('','','','D','A','T','A','','Q','','','','','','E','X','C','E','L');// The length of the row and letters WILL change
L[4] = new Array('','','','','C','','','','U','','','','','','','','','R','');// The length of the row and letters WILL change
L[5] = new Array('','','','','R','','Q','U','A','N','T','I','T','A','T','I','V','E','');// The length of the row and letters WILL change
L[6] = new Array('','','','','O','','','','L','','','','','','','','','C','');// The length of the row and letters WILL change
L[7] = new Array('','','','','S','T','A','T','I','S','T','I','C','S','','','','O','');// The length of the row and letters WILL change
L[8] = new Array('','','','','S','','','','T','','','','','','','','','R','');// The length of the row and letters WILL change
L[9] = new Array('','','','','','','','','A','','','','L','E','G','E','N','D','');// The length of the row and letters WILL change
L[10] = new Array('','I','N','F','O','R','M','A','T','I','O','N','','','','','','E','');// The length of the row and letters WILL change
L[11] = new Array('','','','','','','','','I','','','','P','R','I','M','A','R','Y');// The length of the row and letters WILL change
L[12] = new Array('','','','','','','','','V','','','','','','','','','','');// The length of the row and letters WILL change
L[13] = new Array('','','','','','T','I','M','E','L','Y','','','','','','','','');// The length of the row and letters WILL change

I know what i need to do. I need to use wide cards but thats one thing i am not good at. Everything else (well nearly everything. I am not good at PHP with images.)

I have had a few goes but they have not been close to working so please dont ask me to show you what i have so far.

 

I have been trying to get each parameter (with or without a letter) in an array like

$row[row_number][column_number]='contains of that cell/parameter';

 

If someone can show me how to just do one line that will be great.

 

Thankyou you all

Paul

 

I know it sounds abit open ended and i am sorry for that.

Link to comment
https://forums.phpfreaks.com/topic/175535-rules-that-change-putting-in-too-array/
Share on other sites

I'm not at my development computer, but I think you can turn this into a PHP multi-dimensional array by adding a '$' at the beginning and removing the 'new'; then wrap it in php tags ('<?php', '?>);

<?php
// From L[0] = new Array('','','','W','','','','','','','','','','','','','','T',''); to 
$L[0] = Array('','','','W','','','','','','','','','','','','','','T','');
$L[1] = Array('S','E','C','O','N','D','A','R','Y','','R','E','T','R','I','E','V','A','L');
$L[2] = Array('','','','R','','','','','','','','','','','','','','P','');
$L[3] = Array('','','','D','A','T','A','','Q','','','','','','E','X','C','E','L');
$L[4] = Array('','','','','C','','','','U','','','','','','','','','R','');
$L[5] = Array('','','','','R','','Q','U','A','N','T','I','T','A','T','I','V','E','');
$L[6] = Array('','','','','O','','','','L','','','','','','','','','C','');
$L[7] = Array('','','','','S','T','A','T','I','S','T','I','C','S','','','','O','');
$L[8] = Array('','','','','S','','','','T','','','','','','','','','R','');
$L[9] = Array('','','','','','','','','A','','','','L','E','G','E','N','D','');
$L[10] = Array('','I','N','F','O','R','M','A','T','I','O','N','','','','','','E','');
$L[11] = Array('','','','','','','','','I','','','','P','R','I','M','A','R','Y');
$L[12] = Array('','','','','','','','','V','','','','','','','','','','');
$L[13] = Array('','','','','','T','I','M','E','L','Y','','','','','','','','');
?>

That will give you a nice PHP array named $L with rows and columns.  So

$row = 7;
$col = 6;
echo $L[$row][$col]; // will print: A

i know how to use PHP variables but the code i gave you is javascript.

 

I have got my PHP script which collects the html source code from a different page and selects the javascript part.

 

I then need my php script to "parse" (i think thats the right word) so then i can use it in my other PHP scripts.

 

Thankyou all

Paul

Sorry, your post said you wanted to convert that to PHP.  So now you say you are extracing the javascript code from a file.  So I guess you end up with (in PHP):

$lines[0] = "L[0] = new Array('','','','W','','','','','','','','','','','','','','T','');"
$lines[1] = "L[1] = new Array('S','E','C','O','N','D','A','R','Y','','R','E','T','R','I','E','V','A','L');"
// and so on ...

is that right? 

 

But I don't understand what you want to do with it now.  If you just want to make it into PHP code (like what I gave you) that you can run:

foreach ($lines as $ind => $line) {
  $lines[$ind] = '$' . str_replace(' new', '', $line);
} 

 

or are you trying to keep it as Javascript and just send it to other files?

 

or do you want to generate that javascript code using PHP?

 

Can you explain - or possibly show - what you want the result to be?

Something like this maybe?

 

<?php
$str = "L[0] = new Array('','','','W','','','','','','','','','','','','','','T','');// The length of the row and letters WILL change
L[1] = new Array('S','E','C','O','N','D','A','R','Y','','R','E','T','R','I','E','V','A','L');// The length of the row and letters WILL change
L[2] = new Array('','','','R','','','','','','','','','','','','','','P','');// The length of the row and letters WILL change
L[3] = new Array('','','','D','A','T','A','','Q','','','','','','E','X','C','E','L');// The length of the row and letters WILL change
L[4] = new Array('','','','','C','','','','U','','','','','','','','','R','');// The length of the row and letters WILL change
L[5] = new Array('','','','','R','','Q','U','A','N','T','I','T','A','T','I','V','E','');// The length of the row and letters WILL change
L[6] = new Array('','','','','O','','','','L','','','','','','','','','C','');// The length of the row and letters WILL change
L[7] = new Array('','','','','S','T','A','T','I','S','T','I','C','S','','','','O','');// The length of the row and letters WILL change
L[8] = new Array('','','','','S','','','','T','','','','','','','','','R','');// The length of the row and letters WILL change
L[9] = new Array('','','','','','','','','A','','','','L','E','G','E','N','D','');// The length of the row and letters WILL change
L[10] = new Array('','I','N','F','O','R','M','A','T','I','O','N','','','','','','E','');// The length of the row and letters WILL change
L[11] = new Array('','','','','','','','','I','','','','P','R','I','M','A','R','Y');// The length of the row and letters WILL change
L[12] = new Array('','','','','','','','','V','','','','','','','','','','');// The length of the row and letters WILL change
L[13] = new Array('','','','','','T','I','M','E','L','Y','','','','','','','','');// The length of the row and letters WILL change";

$lines = explode("\n", $str);
$count = 0;

foreach($lines as $v) {

$start = strpos($v, "(");
$end = strpos($v, ")");
$len = $end - $start;
$data = substr($v, $start + 1, $len - 1);
$fieldoutput = "";
$fields = explode(",", $data);

foreach($fields as $v) {
	$fieldoutput[] = str_replace("'", "", $v);

}

$output[] = $fieldoutput;

}

echo "<pre>";
print_r($output);
echo "</pre>";

?>

paulman888888,

 

 

    I created a file called 'lettersArray.js' and put it in my 'javascript' folder :

 


/* lettersArray in javascript format */


L[0] = new Array('','','','W','','','','','','','','','','','','','','T','');					// The length of the row and letters WILL change
L[1] = new Array('S','E','C','O','N','D','A','R','Y','','R','E','T','R','I','E','V','A','L');	// The length of the row and letters WILL change
L[2] = new Array('','','','R','','','','','','','','','','','','','','P','');					// The length of the row and letters WILL change
L[3] = new Array('','','','D','A','T','A','','Q','','','','','','E','X','C','E','L');			// The length of the row and letters WILL change
L[4] = new Array('','','','','C','','','','U','','','','','','','','','R','');					// The length of the row and letters WILL change
L[5] = new Array('','','','','R','','Q','U','A','N','T','I','T','A','T','I','V','E','');		// The length of the row and letters WILL change
L[6] = new Array('','','','','O','','','','L','','','','','','','','','C','');					// The length of the row and letters WILL change
L[7] = new Array('','','','','S','T','A','T','I','S','T','I','C','S','','','','O','');			// The length of the row and letters WILL change
L[8] = new Array('','','','','S','','','','T','','','','','','','','','R','');					// The length of the row and letters WILL change
L[9] = new Array('','','','','','','','','A','','','','L','E','G','E','N','D','');				// The length of the row and letters WILL change
L[10] = new Array('','I','N','F','O','R','M','A','T','I','O','N','','','','','','E','');		// The length of the row and letters WILL change
L[11] = new Array('','','','','','','','','I','','','','P','R','I','M','A','R','Y');			// The length of the row and letters WILL change
L[12] = new Array('','','','','','','','','V','','','','','','','','','','');					// The length of the row and letters WILL change
L[13] = new Array('','','','','','T','I','M','E','L','Y','','','','','','','','');				// The length of the row and letters WILL change

this.varName = 'Some Other JS Statement';

 

Below is a script which will read the .js file, and produce a PHP array of the values L[n] -> L[final]...

 

I hope this is what you were hoping to do.

 

 

Scot L. Diddle, Richmond VA

 

<?php

$myLetterArray = file('javascript/lettersArray.js');  // Creates a array, each element = on row of the input file...

$outputArray   = array();

// Create: $row[row_number][column_number] = value_found

foreach ($myLetterArray as $IDX => $lineItem) {

	$lineItem = trim($lineItem);

	$hasTarget = stristr($lineItem, 'L['); 

	if ($hasTarget) {

		$lineItemPieces = explode(',', $lineItem);

		$arrayIndex = $lineItemPieces[0]; // L[0] = new Array(''

		$arrayIndexPieces = explode(' ', $arrayIndex);   // $arrayIndexPieces[0] = 'L[0]'
		 												 // $arrayIndexPieces[1] = '='
		  												 // $arrayIndexPieces[2] = 'new'
		 												 // $arrayIndexPieces[3] = 'Array('first_value'
		$myRowID = $arrayIndexPieces[0]; // 'L[0]'

		$myRowID = str_ireplace('[', '_', $myRowID);
		$myRowID = str_ireplace(']', '', $myRowID);

		$myFirstValuePieces = explode('(', $arrayIndexPieces[3] );   //  $myFirstValuePieces[0] = 'Array'
																	 //  $myFirstValuePieces[1] = 'first_value'

		$myFirstValue = $myFirstValuePieces[1];	  //  $myFirstValuePieces[1] = 'first_value'

		$myLastItem = array_pop($lineItemPieces); // '');					// The length of the row and letters WILL change

		$myLastItemPieces = explode(' ', $myLastItem); 		 // $myLastItemPieces[0] = "'');"

		$myLastItemPiece2 = explode(')', $myLastItemPieces[0]); //  $myLastItemPieces2[0] = "''"

		$myLastValue = $myLastItemPiece2[0];

		$outputArray[$myRowID][] = $myFirstValue; //  $myFirstValuePieces[1] = 'first_value'

		// Get rid of the leading $lineItemPieces ( [0] ... already handled...

		array_shift($lineItemPieces);

		foreach($lineItemPieces as $IDX2 => $characterItem) {
			$outputArray[$myRowID][] = $characterItem;
		}

		$outputArray[$myRowID][] = $myLastValue; //  $myLastItemPiece2[0];

	}


}

echo "<pre> \n"	;

print_r($outputArray);

echo "</pre> \n";


/**
 * 
 *   Should produce: 
 * 
 */

/*

	Array
	(
	    [L_0] => Array
	        (
	            [0] => ''
	            [1] => ''
	            [2] => ''
	            [3] => 'W'
	            [4] => ''
	            [5] => ''
	            [6] => ''
	            [7] => ''
	            [8] => ''
	            [9] => ''
	            [10] => ''
	            [11] => ''
	            [12] => ''
	            [13] => ''
	            [14] => ''
	            [15] => ''
	            [16] => ''
	            [17] => 'T'
	            [18] => ''
	        )

	    [L_1] => Array
	        (
	            [0] => 'S'
	            [1] => 'E'
	            [2] => 'C'
	            [3] => 'O'
	            [4] => 'N'
	            [5] => 'D'
	            [6] => 'A'
	            [7] => 'R'
	            [8] => 'Y'
	            [9] => ''
	            [10] => 'R'
	            [11] => 'E'
	            [12] => 'T'
	            [13] => 'R'
	            [14] => 'I'
	            [15] => 'E'
	            [16] => 'V'
	            [17] => 'A'
	            [18] => 'L'
	        )
		...

	    [L_12] => Array
	        (
	            [0] => ''
	            [1] => ''
	            [2] => ''
	            [3] => ''
	            [4] => ''
	            [5] => ''
	            [6] => ''
	            [7] => ''
	            [8] => 'V'
	            [9] => ''
	            [10] => ''
	            [11] => ''
	            [12] => ''
	            [13] => ''
	            [14] => ''
	            [15] => ''
	            [16] => ''
	            [17] => ''
	            [18] => ''
	        )

	    [L_13] => Array
	        (
	            [0] => ''
	            [1] => ''
	            [2] => ''
	            [3] => ''
	            [4] => ''
	            [5] => 'T'
	            [6] => 'I'
	            [7] => 'M'
	            [8] => 'E'
	            [9] => 'L'
	            [10] => 'Y'
	            [11] => ''
	            [12] => ''
	            [13] => ''
	            [14] => ''
	            [15] => ''
	            [16] => ''
	            [17] => ''
	            [18] => ''
	        )
	        
*/

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.