Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

first part of parsing many lines

Posted on 2013-06-19
6
224 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 109

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 7

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

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 109

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

808 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