0

I am trying to extract values from XML.My xml file contains a field for product "Description" that consists of Product bardcode, packets and pallets.

I want to extract only Pck (it could be any number from 1-999).

My xml attribute(Description) field values:

  Description="5038135129483  
&#xAPck: 4   Plt: 120" 

Here

 Barcode=5038135129483   
 Number of Packets(Pck)=4
 Number of Palletes(Plt)=120

 $pieces = substr($WhatDescription,19,20);

I have tried substr function to extract from string but it doesn't give me accurate results as the barcode 's length varies and I have got html characters in my XML. Could you advise what shall I do to extract Pck from above string in such away that my extraction wont depend upon html characters and barcode's length.

2
  • ca2.php.net/manual/en/ref.pcre.php Commented Nov 23, 2012 at 16:53
  • 1
    You should be using an XML parser, not regexes. Commented Nov 23, 2012 at 16:55

1 Answer 1

1

You can try:

<?php
   $text = "5038135129483  &#xD;&#xAPck: 4   Plt: 120";
   $newtext = strstr($text,'Pck:');
   echo $final = trim(substr($newtext,0,stripos($newtext,'Plt:')));
?>

Working code: http://codepad.org/3sbfIim8

Sign up to request clarification or add additional context in comments.

4 Comments

@GBDthanks.But this code retrieves anything that comes after "4" as well.How can avoid this in my string??
can you paste your complete string ?
@GBD.Thanks.here it is: $text="5038135129483 &#xD;&#xAPck: 4 &nbsp; Plt: 120 itemcode:91";
You can use strip_tags() and replace &nbsp; here is example: codepad.org/iuc5piWv

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.