Solved

Php code to print between two delimiters

Posted on 2006-06-21
10
320 Views
Last Modified: 2008-01-16
Hi,
Can someone help me convert this PERl to PHP?

Perl: (replace START and END to print everything between the two)
perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs'

Php:
???
0
Comment
Question by:cdukes
  • 7
  • 2
10 Comments
 

Author Comment

by:cdukes
ID: 16955831
Ok, so I have this, but it doesn't seem to work -- it only prints out the first 3 parts of the array????


// Example: printbetween("<title>", "<\/title>", $html_content);
function printbetween($start,$end,$data) {
    // preg_match("/$start(.*)$end/s",$data,$match);
    preg_match("/$start(.*?)$end/i",$data,$match);
    return $match;
}

    $TT = printbetween("<TT>", "<\/TT>", $res);
   foreach ($TT as $v) {
        echo "Data:: " .htmlspecialchars($v) ."\n<br>";
    }      


And this returns:
Data:: <TT><SPAN CLASS="contentbold">%CDP-3-PKTRECVFAIL:</SPAN></TT>
Data:: <SPAN CLASS="contentbold">%CDP-3-PKTRECVFAIL:</SPAN>
Data::

But it SHOULD be returning:

<TT><SPAN CLASS="contentbold">%CDP-3-PKTRECVFAIL:</SPAN></TT>
<TT> Failed to receive packet - [chars].</TT>
0
 

Author Comment

by:cdukes
ID: 16956461
For clarification, here's the page: I need to extract the following:

Error Message: %CDP-3-PKTRECVFAIL
Short Desc: Failed to receive packet - [chars].
Long Desc: Failed to receive a packet because of the reason shown in the error message.
Recommended Action: No action is required. Introduced Cisco MDS SAN-OS Release 1.2(2a).
Related documents: - No specific documents apply to this error message
Note that sometimes more than one result may be returned for the search, for example: if I just search on "CDP-", the page would return about 55 entries.

And here's the relevant html:

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" CLASS="appstablecolor">
   <TR>
      <TD WIDTH="6"><IMG SRC="/swa/i/spacer.gif" WIDTH="6" HEIGHT="17" ALT=""></TD>
      <TD WIDTH="99%" NOWRAP><IMG SRC="/swa/i/spacer.gif" WIDTH="1" HEIGHT="1" ALT=""><BR>
         <SPAN CLASS="contentheaderrev">Showing 1-1 of 1 results</SPAN>
         <IMG SRC="/swa/i/spacer.gif" WIDTH="1" HEIGHT="1" ALT=""></TD>
         <TD ALIGN="right" NOWRAP>
         <SPAN CLASS="contentheaderrev">
             
             Page:
             <SELECT NAME="select" onChange="JumpTo(this)">
             <OPTION VALUE="/cgi-bin/Support/Errordecoder/index.cgi?action=search&locale=en&query=CDP-3-PKTRECVFAIL&counter=0&paging=5&links=reference&index=all" SELECTED>1</OPTION>

             </SELECT>
             

         </SPAN>
      </TD>
      <TD WIDTH="7" ALIGN="right" VALIGN="top"><IMG SRC="/swa/i/corner_ur_7.gif" WIDTH="7" HEIGHT="7" ALT=""></TD>    
   </TR>
</TABLE>

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" CLASS="appstablecolor">
  <TR>
    <TD>
      <TABLE BORDER="0" CELLSPACING="1" CELLPADDING="5" WIDTH="100%">
      
      <TR BGCOLOR="#ffffff">
            <TD><SPAN CLASS="contentbold">1.</SPAN>
            <TT><SPAN CLASS="contentbold">%CDP-3-PKTRECVFAIL:</SPAN></TT>
            <TT> Failed to receive packet - [chars].</TT>
            <P>Failed to receive a packet because of the reason shown in the error message.<BR><BR>
            <B>Recommended Action:
</B>No action is required.
Introduced  Cisco MDS SAN-OS Release 1.2(2a).<BR>
            
                  
            <BR><B>Related documents- </B>No specific documents apply to this error message.
            <BR>
                  
                  
            </TD>
      </TR>
      

      </TABLE>
    </TD>
  </TR>
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" CLASS="appstablecolor">
   <TR>
     <TD WIDTH="6"><IMG SRC="/swa/i/spacer.gif" WIDTH="6" HEIGHT="17" ALT=""></TD>
     <TD WIDTH="99%" NOWRAP><IMG SRC="/swa/i/spacer.gif" WIDTH="1" HEIGHT="1" ALT=""><BR>
       <SPAN CLASS="contentheaderrev">Showing 1-1 of 1 results</SPAN></TD>
       <TD ALIGN="right" NOWRAP>
       <SPAN CLASS="contentheaderrev">
      
         Page:
         <SELECT NAME="select" onChange="JumpTo(this)">
       <OPTION VALUE="/cgi-bin/Support/Errordecoder/index.cgi?action=search&locale=en&query=CDP-3-PKTRECVFAIL&counter=0&paging=5&links=reference&index=all" SELECTED>1</OPTION>

         </SELECT>
      
       </SPAN>
     </TD>
   </TR>
 </TABLE>



0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16957815
Try changing this line
  preg_match("/$start(.*?)$end/i",$data,$match);
into
  preg_match_all("/$start(.*?)$end/i",$data,$match);

Then to get the array you want:
$final_match = array();
foreach($matches as $match) {
    $final_match[] = $match[0];
}
print_r($final_match);
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:cdukes
ID: 16958988
That yeilds the same results...

function printbetween($start,$end,$data) {
    // preg_match("/$start(.*)$end/",$data,$match);
    preg_match_all("/$start(.*?)$end/i",$data,$match);
    return $match;
}        


   $TT = printbetween("<TT>", "<\/TT>", $res);
    $final_match = array();
    foreach($TT as $match) {
            $final_match[] = $match[0];
    }
    print_r($final_match);

Yeilds:
Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => %CDP-3-PKTRECVFAIL: )
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16959517
Can you add this line:
print_r($TT);

between these lines:
   $TT = printbetween("<TT>", "<\/TT>", $res);
    $final_match = array();

and show me the result?
0
 

Author Comment

by:cdukes
ID: 16960253
k, here's what I did (removed the function "printbetween" in case it was causing the problem):

    $TT = preg_match_all("/<TT>(.*?)<\/TT>/i",$res,$match);
    echo "PRINT_R:<br>";
    print_r($match);
    echo "<BR>END PRINT_R:<br>";
    $final_match = array();
    echo "Foreach Loop: <br>";
    foreach($match as $matches) {
        $final_match[] = $matches[0];
    }
    print_r($final_match);


Output:
Array ( [0] => Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => Failed to receive packet - [chars]. ) [1] => Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => Failed to receive packet - [chars]. ) )
END PRINT_R:
Foreach Loop:
Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => %CDP-3-PKTRECVFAIL: )


0
 

Author Comment

by:cdukes
ID: 16960297
Eureka, I think I have it!

    $TT = preg_match_all("/<TT>(.*?)<\/TT>/i",$res,$match);
    echo "<br>";
    echo "Match: " .$match[0][0];
    echo "<br>";
    echo "Match2: " . $match[0][1];
    echo "<br>";
    echo "<br>";

Yeilds:

Match: %CDP-3-PKTRECVFAIL:
Match2: Failed to receive packet - [chars].
0
 

Author Comment

by:cdukes
ID: 16960458
Hrm...oddly enough, if I take the following:

    print "<b><font color=\"green\">END REPLY ARRAY</b></font><br>\n";
    $TT = preg_match_all("/<TT>(.*?)<\/TT>/i",$res,$match);
    echo "PRINT_R:<br>";
    print_r($match);
    echo "<BR>END PRINT_R:<br>";
    $final_match = array();
    echo "Foreach Loop: <br>";
    foreach($match as $matches) {
        $final_match[] = $matches[0];      
    }                                          
    print_r($final_match);        


Then it prints everything between <TT> and </TT>
Output:
PRINT_R:
Array ( [0] => Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => Failed to receive packet - [chars]. ) [1] => Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => Failed to receive packet - [chars]. ) )
END PRINT_R:
Foreach Loop:
Array ( [0] => %CDP-3-PKTRECVFAIL: [1] => %CDP-3-PKTRECVFAIL: )



But, if I widen it to print everything between <FORM> and </FORM> it doesn't work, why is that?
Output:
PRINT_R:
Array ( [0] => Array ( ) [1] => Array ( ) )
END PRINT_R:
Foreach Loop:
Array ( [0] => [1] => )

0
 
LVL 5

Accepted Solution

by:
aminerd earned 500 total points
ID: 16964998
By default the period (.) does not match new lines. You can modify this behaviour with the s (PCRE_DOTALL) modifier:

$TT = preg_match_all("/<FORM>(.*?)<\/FORM>/is", $res, $match);
0
 

Author Comment

by:cdukes
ID: 16975401
I've come to the realization that this question needs more information.
To that end, I've posted a new request to start clean, so to speak.
Please see:
http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21897932.html

for the new post.

(Note to Moderator, please remove this post at your earliest convenience).
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 63
Currency in SQL? 2 33
How to delete values from an array using php 4 24
Log in through ID 5 19
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …

820 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