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.

jonboy
  • 2,729
  • 6
  • 37
  • 77

3 Answers3

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

Community
  • 1
  • 1
Orry
  • 659
  • 6
  • 21
  • Thanks but I don't have access to the external URL in order to format the text into `` tags – jonboy Sep 29 '15 at 09:36
0
$var = $element->innertext;
$array = explode('<br>', $var);
echo $array[31];
er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29
  • 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) "" }` – jonboy Sep 29 '15 at 09:53
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);
Fedir Petryk
  • 497
  • 3
  • 16