0

I am trying to select a particular part of a html page using 'Simple HTML DOM Parser'.

My code so far works but it returns the complete text which is great, but I only want to display the single P BARCODE[pb] line.

My (working) PHP code is as follows;

$homepage = file_get_html('http://example.com/SomeTextPage');

foreach($homepage->find('text') as $element) 
       echo $element->innertext . '<br>'; // line 29

This returns the following on my page (this is the view source display);

<HTML>
<BODY>
EXP DATE[p43]=12-31-97<BR>
PCODE1[p44]=-<BR>
PCODE2[p45]=-<BR>
PCODE3[p46]=0<BR>
P TYPE[p47]=1<BR>
TOT CHKOUT[p48]=56<BR>
TOT RENWAL[p49]=17<BR>
CUR CHKOUT[p50]=3<BR>
HOME LIBR[p53]=0000<BR>
PMESSAGE[p54]=<BR>
MBLOCK[p56]=-<BR>
REC TYPE[p80]=p<BR>
RECORD #[p81]=110220<BR>
REC LENG[p82]=1126<BR>
CREATED[p83]=01-09-97<BR>
UPDATED[p84]=06-05-97<BR>
REVISIONS[p85]=139<BR>
AGENCY[p86]=1<BR>
CL RTRND[p95]=0<BR>
MONEY OWED[p96]=$1.35<BR>
BLK UNTIL[p101]=  -  -  <BR>
CUR ITEMA[p102]=0<BR>
CUR ITEMB[p103]=0<BR>
PIUSE[p104]=0<BR>
OD PENALTY[p105]=0<BR>
ILL CHKOUT[p122]=3<BR>
PATRN NAME[pn]=Jackson, Richard<BR>
ADDRESS[pa]=322 San Diego St<BR>
ADDRESS2[ph]=El Cerrito, CA 99999<BR>
TELEPHONE[pt]=510-555-1212<BR>
UNIV ID[pu]=111111111<BR>
P BARCODE[pb]=21913000482538<BR>
</BODY>
</HTML>

I suppose I need to select the 32nd <br> line or more importantly the [P BARCODE[pb]] line - is this possible?

The [P BARCODE[pb]] line isn't always the 32nd line, but the [P BARCODE[pb]] text never changes.

Perhaps I am approaching this the wrong way?

Any help or suggestions are welcome.

4
  • 1
    I would try $homepage->find('text')[31], or is that too easy? Commented Sep 29, 2015 at 9:35
  • is it line no 32nd fixed Commented Sep 29, 2015 at 9:36
  • @MrLister that doesn't work. I get Notice: Trying to get property of non-object in C:\xampp\... on line 29. Line 29 is my echo Commented Sep 29, 2015 at 9:38
  • @Irfan that's a good point, no it sometimes changes between line 31 and 32. The text [P BARCODE[pb] never changes though. Commented Sep 29, 2015 at 9:40

3 Answers 3

0

Would you like to try to use regexp?

$subject = 'P BARCODE[pb]=21913000482538<BR>';
$pattern = '/P\sBARCODE\[pb\]=([0-9]*)<BR>/'; 
preg_match($pattern, $subject, $matches); 
print_r($matches);
Sign up to request clarification or add additional context in comments.

Comments

0
$var = $element->innertext;
$array = explode('<br>', $var);
echo $array[31];

1 Comment

Thanks @Irfan but that doesn't work. I get Notice: Undefined offset: 31 in C:\xampp... on line 32. Line 32 being echo $array[31];. a var_dump of $array shows array(2) { [0]=> string(1) " " [1]=> string(0) "" }
0

It doesn't work like that, wrap every line of text in a <span> so you can access them individually.

Apperantly there is another option, I'd like to refer to this answer

1 Comment

Thanks but I don't have access to the external URL in order to format the text into <span> tags

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.