Solved

first part of parsing many lines

Posted on 2013-06-19
6
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

910 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

23 Experts available now in Live!

Get 1:1 Help Now