1

I have a string like this

$str=44797675962044781781414912/04/2012
  • First 12 digit: 447976759620 is the phone number
  • Next 12 digit: 447817814149 is the server code

and rest are the date

I am trying to get it like this:

echo substr($str,0,12).'<br/>';
echo substr($str,12,24).'<br/>';
$tempRest["receivedtime"]=substr($pieces[0],-10);

Getting this weird out put :

   44797675962
   12/04/2012
   44778691004444781781414912/04/2012

What can I do. Any help?

EDITED :

Original code :

  function GetSoapReturnArray() {
 $classmap = array('MyWSDLStructure' => 'MyComplexDataType');
 $m_wsdl = 'https://m2mconnect.orange.co.uk/orange-soap/services/MessageServiceByCountry?wsdl';
  $m_arr_soapclient = array('trace' => true, 'exceptions' => true);


  $soap_client_handle = new SoapClient($m_wsdl, $m_arr_soapclient);
  $soap_result = $soap_client_handle->peekMessages('telab048', 'Qwerty12', 40, '', 1);;

  $str=NULL;
  
  foreach ($soap_result as $soapArrayData)
  {
    $pieces = preg_split('/[ ]/', $soapArrayData);

  $str=$pieces[0];
    //echo $str.'<br/>';
    
    echo substr($str, 0, 12) . '<br />';
    echo substr($str, 12, 12) . '<br />';
  }
}


}
  GetSoapReturnArray();

  
2
  • 1
    You get $str from where? XML? Commented Apr 20, 2012 at 18:54
  • seems its XML response. Did you try DOM? Commented Apr 20, 2012 at 19:12

3 Answers 3

4

The second parameter to substr is the length, not the ending index. Pass 12 instead of 24.

echo substr($str, 0, 12) . '<br />';
echo substr($str, 12, 12) . '<br />';
$tempRest["receivedtime"] = substr($pieces[0], 24);
Sign up to request clarification or add additional context in comments.

5 Comments

no luck the output is like this :44797675962044781781414912/04/2012 < sourcemsisdn
the string is coming from a webservice..but this shouldn't be the problem..let me show u what the original code is...
@sohel14_cse_ju: You figured out what the string contained by using echo $str . '<br />';, right? Well, it may have contained XML that your browser did not display (it being part of the structure). Try "View Source" on that output, and I think you'll find some unexpected things.
ya it was returning xml..i jsut made a parser and that worked..thanks
0

you are having problems because $str is not just a string it contains HTML tags use strip_tags to filter all the html tags ...

Try

    foreach ( $soap_result as $soapArrayData ) {
        $pieces = preg_split ( '/[ ]/', $soapArrayData );

        $str = $pieces [0];
        $str = trim ( $str );
        $str = strip_tags ( $str );
        echo substr ( $str, 0, 12 ) . '<br />'; //Phone Number
        echo substr ( $str, 12, 12 ) . '<br />'; // Server Code
        echo substr ( $str, 24, 10 ) . '<br />'; //Time
    }

Comments

0

For that you have sscanf:

$r = sscanf($str, '%12s%12s%s', $phone, $server, $date);

Output (var_dump($phone, $server, $date);):

string(12) "447976759620"
string(12) "447817814149"
string(10) "12/04/2012"

Comments

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.