cdukes
asked on
Php code to print between two delimiters
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:
???
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:
???
ASKER
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">S howing 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/Er rordecoder /index.cgi ?action=se arch&local e=en&query =CDP-3-PKT RECVFAIL&c ounter=0&p aging=5&li nks=refere nce&index= all" SELECTED>1</OPTION>
</SELECT>
</SPAN>
</TD>
<TD WIDTH="7" ALIGN="right" VALIGN="top"><IMG SRC="/swa/i/corner_ur_7.gi f" 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.</SP AN>
<TT><SPAN CLASS="contentbold">%CDP-3 -PKTRECVFA IL:</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">S howing 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/Er rordecoder /index.cgi ?action=se arch&local e=en&query =CDP-3-PKT RECVFAIL&c ounter=0&p aging=5&li nks=refere nce&index= all" SELECTED>1</OPTION>
</SELECT>
</SPAN>
</TD>
</TR>
</TABLE>
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">S
<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/Er
</SELECT>
</SPAN>
</TD>
<TD WIDTH="7" ALIGN="right" VALIGN="top"><IMG SRC="/swa/i/corner_ur_7.gi
</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.</SP
<TT><SPAN CLASS="contentbold">%CDP-3
<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">S
<TD ALIGN="right" NOWRAP>
<SPAN CLASS="contentheaderrev">
Page:
<SELECT NAME="select" onChange="JumpTo(this)">
<OPTION VALUE="/cgi-bin/Support/Er
</SELECT>
</SPAN>
</TD>
</TR>
</TABLE>
Try changing this line
preg_match("/$start(.*?)$e nd/i",$dat a,$match);
into
preg_match_all("/$start(.* ?)$end/i", $data,$mat ch);
Then to get the array you want:
$final_match = array();
foreach($matches as $match) {
$final_match[] = $match[0];
}
print_r($final_match);
preg_match("/$start(.*?)$e
into
preg_match_all("/$start(.*
Then to get the array you want:
$final_match = array();
foreach($matches as $match) {
$final_match[] = $match[0];
}
print_r($final_match);
ASKER
That yeilds the same results...
function printbetween($start,$end,$ data) {
// preg_match("/$start(.*)$en d/",$data, $match);
preg_match_all("/$start(.* ?)$end/i", $data,$mat ch);
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: )
function printbetween($start,$end,$
// preg_match("/$start(.*)$en
preg_match_all("/$start(.*
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: )
Can you add this line:
print_r($TT);
between these lines:
$TT = printbetween("<TT>", "<\/TT>", $res);
$final_match = array();
and show me the result?
print_r($TT);
between these lines:
$TT = printbetween("<TT>", "<\/TT>", $res);
$final_match = array();
and show me the result?
ASKER
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,$matc h);
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: )
$TT = preg_match_all("/<TT>(.*?)
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: )
ASKER
Eureka, I think I have it!
$TT = preg_match_all("/<TT>(.*?) <\/TT>/i", $res,$matc h);
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].
$TT = preg_match_all("/<TT>(.*?)
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].
ASKER
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,$matc h);
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] => )
print "<b><font color=\"green\">END REPLY ARRAY</b></font><br>\n";
$TT = preg_match_all("/<TT>(.*?)
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] => )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:
https://www.experts-exchange.com/questions/21897932/Extract-messages-from-HTML-and-store-in-MySQL.html
for the new post.
(Note to Moderator, please remove this post at your earliest convenience).
To that end, I've posted a new request to start clean, so to speak.
Please see:
https://www.experts-exchange.com/questions/21897932/Extract-messages-from-HTML-and-store-in-MySQL.html
for the new post.
(Note to Moderator, please remove this post at your earliest convenience).
ASKER
// Example: printbetween("<title>", "<\/title>", $html_content);
function printbetween($start,$end,$
// preg_match("/$start(.*)$en
preg_match("/$start(.*?)$e
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
Data:: <SPAN CLASS="contentbold">%CDP-3
Data::
But it SHOULD be returning:
<TT><SPAN CLASS="contentbold">%CDP-3
<TT> Failed to receive packet - [chars].</TT>