Solved

first part of parsing many lines

Posted on 2013-06-19
6
219 Views
Last Modified: 2013-06-24
Ray's code works, but I want to know why

I want to discover what the first part does
I notice that the code breaks when I run

line
23
first-start:
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;<br>first-end<br><br /> <b>Notice</b>: Undefined offset: 0 in <b>C:\Users\Acer\Documents\NuSphere PhpED\Projects\ray-website-parse2.php</b> on line <b>25</b><br /> <br>line<br>25<br>first-start:<br><br>first-end<br>Array ( [0] => want-this-number [1] => want-this-number [2] => want-this-number [3] => want-this-number [4] => want-this-number ) <br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-number

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);
echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';
unset($arr[0]);
echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';

// USE THE STOP SIGNAL TO DECLOP THE ELEMENTS OF THE ARRAY
foreach ($arr as $sub)
{
    $sub = explode($z, $sub);
    $out[] = $sub[0];
}

// SHOW THE EXTRACTED SUBSTRINGS
print_r($out);
foreach ($out as $line){
  echo '<br>'.$line;
}

Open in new window

0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39260123
Ray's code works, but I want to know why
I wish I could say "because I have magic powers," but that's not the case.  I just am very lazy and hate debugging so I debug code almost always one or two lines at a time.

The key parts of the script are the explode() statements.

You can use var_dump() to print out the intermediate variables.  You may want to use view-source in your browser, in case the intermediate variables contain any HTML.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);

// SHOW THE ARRAY AFTER BREAKING THE STRING
var_dump($arr);

echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';
unset($arr[0]);
echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';

// SHOW THE ARRAY AFTER DISCARDING THE TOP POSITION
var_dump($arr);

// USE THE STOP SIGNAL TO DECLOP THE ELEMENTS OF THE ARRAY
foreach ($arr as $sub)
{
    $sub = explode($z, $sub);
    $out[] = $sub[0];
}

// SHOW THE EXTRACTED SUBSTRINGS
print_r($out);
foreach ($out as $line){
  echo '<br>'.$line;
}

Open in new window

0
 
LVL 6

Assisted Solution

by:Robert Saylor
Robert Saylor earned 250 total points
ID: 39260380
The error is because you are deleting your array then referencing it after the unset. Here is the corrected code with the array error checking.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);
echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';
if (is_array($arr)) {
        unset($arr[0]);
}



if(isset($arr[0])) {
        echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';
}

// USE THE STOP SIGNAL TO DECLOP THE ELEMENTS OF THE ARRAY
foreach ($arr as $sub)
{
    $sub = explode($z, $sub);
    $out[] = $sub[0];
}

// SHOW THE EXTRACTED SUBSTRINGS
print_r($out);
foreach ($out as $line){
  echo '<br>'.$line;
}
?>

Open in new window


Output:

root@mx [~]# php test.php
<br>line<br>23<br>first-start:<br>text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;<br>first-end<br>
Notice: Undefined offset:  0 in /root/test.php on line 30
<br>line<br>30<br>first-start:<br><br>first-end<br>Array
(
    [0] => want-this-number
    [1] => want-this-number
    [2] => want-this-number
    [3] => want-this-number
    [4] => want-this-number
)
<br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-numberroot@mx [~]# vim test.php

After adding error checking:

root@mx [~]# php test.php
<br>line<br>23<br>first-start:<br>text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;<br>first-end<br>Array
(
    [0] => want-this-number
    [1] => want-this-number
    [2] => want-this-number
    [3] => want-this-number
    [4] => want-this-number
)
<br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-number<br>want-this-numberroot@mx [~]# vim test.php
root@mx [~]#
0
 

Author Comment

by:rgb192
ID: 39261179
Ray putting
var_dump($arr);
twice
prevented the code from crashing after the

echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';


I do not understand why

I can understand an if statement from Rsaylor

if (is_array($arr)) {
        unset($arr[0]);
}



if(isset($arr[0])) {
        echo '<br>line<br>'.__LINE__.'<br>first-start:<br>'.$arr[0].'<br>first-end<br>';
}
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:Robert Saylor
ID: 39262246
The error you were getting "crash" was it this?

Notice: Undefined offset:  0

If so, that error means you called an array "$arr[0]" after the array was unset or deleted. The error means the array did not exist when you called "$arr[0]"

Let me know if you need further help.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39262359
@rsaylor: I'm not sure that I understand the behavior.  Here is what I am seeing -- code, then output of "view source.  Note the print_r() is commented out, and the "Foo" never appears:
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);

echo
  '<br>line<br>'
. __LINE__
. '<br>first-start:<br>'
. $arr[0]
. '<br>first-end<br>'
;

// print_r($arr);

echo PHP_EOL . 'Foo';

Open in new window

Outputs:
<br>line<br>26<br>first-start:<br>text here

Open in new window

And this version which includes the print_r():
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);

echo
  '<br>line<br>'
. __LINE__
. '<br>first-start:<br>'
. $arr[0]
. '<br>first-end<br>'
;

print_r($arr);

echo PHP_EOL . 'Foo';

Open in new window

Outputs:
<br>line<br>26<br>first-start:<br>text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;<br>first-end<br>Array
(
    [0] => text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;
    [1] => want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;
    [2] => want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;
    [3] => want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;
    [4] => want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;
    [5] => want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
)

Foo

Open in new window

I don't know why this occurs.  At first, it looked like something maybe related to buffering of the output.  But this script, a little more simplified, fails to echo "Foo" and I do not see any PHP-related syntax that would be in play.  A recondite conundrum.  If you echo an array position that does not exist, causing Undefined offset or Undefined index, the script continues to echo "Foo"
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

ob_start();

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);
echo $arr[0];
echo PHP_EOL . 'Foo';

Open in new window

In the following version, the output is different.  Note the string length.
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

ob_start();

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28158846.html

$txt = <<<EOD
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;l=want-this-number"><img src="/images/start.gif" alt="Start"></a>more text here
EOD;


// START SIGNAL
$a = 'l=';

// STOP SIGNAL
$z = '"';

// MAKE AN ARRAY BY BREAKING STRING ON START SIGNAL
$arr = explode($a, $txt);
var_dump($arr[0]);
echo PHP_EOL . 'Foo';

Open in new window

Outputs (Chrome):
string(76) "text here

Open in new window

However in IE View Source we see:
string(76) "text here<a href="/start.php?a=request-link&amp;s=new-york&amp;f=intake&amp;"

Foo

Open in new window

So it may be that the combination of HTML tag-start and quote marks is confusing the browser.

Sidebar note: Undefined offset means that the array exists, but the numeric key index position did not exist.
0
 

Author Closing Comment

by:rgb192
ID: 39270578
thanks for explanation of output error

and telling me definition of array offset (key, value)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now