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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.