Jump to content

[SOLVED] I need to pull a value from an HTML string.


axiom007

Recommended Posts

The company I work for uses a lot of web services to interact with thier ERP.  However, instead of returning data, it returns html strings.

 

How could I pull the order number out of this html string

<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" align="center" class="pageHeading">Your order has been processed!<br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">Your Order Number is:   0105318598(BBWO1220556726) </span><br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">If you have any questions regarding this order, please contact the  Fulfillment Department at 555-555-5555.<br /><br />Thank you for shopping with!</span></td></tr></table>

 

 

Any help would be appreciated.

Is this what you mean?

 

<?
$text = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" align="center" class="pageHeading">Your order has been processed!<br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">Your Order Number is:   0105318598(BBWO1220556726) </span><br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">If you have any questions regarding this order, please contact the  Fulfillment Department at 555-555-5555.<br /><br />Thank you for shopping with!</span></td></tr></table>';
preg_match_all("/<span class=\"paragraphText\">Your Order Number is:   (.*) <\/span>/", $text, $matches);
print_r($matches[1][0]);
?>

This picks the order number from the text.

 

If you want to retrieve it from a URL you can use

<?
$text = file_get_contents("http://example.com"); //enter url of page here
preg_match_all("/<span class=\"paragraphText\">Your Order Number is:   (.*) <\/span>/", $text, $matches);
print_r($matches[1][0]);
?>

 

If i have misunderstood i apologise

that is real close.  I tried the firstcode you suggested and it pulled the order number, but it also pulled the part in parenthesis, the (BBWO....) part.  I only need the first part.  I am terrible at regex so the suggestion of what I need to add to yours would be appreciated.

 

Thanks

Here:

 

<?php
$text = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" align="center" class="pageHeading">Your order has been processed!<br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">Your Order Number is:   0105318598(BBWO1220556726) </span><br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">If you have any questions regarding this order, please contact the  Fulfillment Department at 555-555-5555.<br /><br />Thank you for shopping with!</span></td></tr></table>';
$matched = array();
preg_match('/(?: )(\d+(?=\())/', $text, $matched);
print_r($matched);
?>

$matched[1] will be your number.

ahh i get you :P

 

<?
$text = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" align="center" class="pageHeading">Your order has been processed!<br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">Your Order Number is:   0105318598(BBWO1220556726) </span><br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">If you have any questions regarding this order, please contact the  Fulfillment Department at 555-555-5555.<br /><br />Thank you for shopping with!</span></td></tr></table>';
preg_match_all("/<span class=\"paragraphText\">Your Order Number is:   (.*)(BBWO[^>]*) <\/span>/", $text, $matches);
$matches[1][0] = str_replace("(", "", $matches[1][0]);
print_r($matches[1][0]);
?>

 

i had to include a string repalce because no matetr what i did there was always a '(' at the end of the string :(

 

hope thats what you want!

Thanks.  Use mine, thread starter.  For your info, Hinty, I used a positive lookahead for the parenthesis.  I could have actually made the regex:

 

<?php
$text = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" align="center" class="pageHeading">Your order has been processed!<br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">Your Order Number is:   0105318598(BBWO1220556726) </span><br /><br /></td></tr><tr><td width="100%" align="center"><span class="paragraphText">If you have any questions regarding this order, please contact the  Fulfillment Department at 555-555-5555.<br /><br />Thank you for shopping with!</span></td></tr></table>';
$matched = array();
preg_match('/(?<= )(\d+(?=\())/', $text, $matched);
print_r($matched);
?>

 

Which would make $matched[0] and $matched[1] equal the number he wants.  In fact, use this one, thread starter.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.