Solved

first part of parsing many lines

Posted on 2013-06-19
6
230 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
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 110

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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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 110

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
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 look for a specific file type in a local or remote server directory using PHP.

626 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