i've stored a line into string $output='01/07/201521/08/201691754.0'; i want split the line as three different words & store into variables var1 var2 and var3.

eg:
var1=01/07/2015
var2=21/08/2016
var3=91754.0

hope someone will fix this issue.

Recommended Answers

$output='01/07/201521/08/201691754.0';

$var1 = substr($output, 0, 9);
$var2 = substr($output, 10, 19);
$var3 = substr($output, 20, 26);

http://php.net/manual/en/function.substr.php

Jump to Post

So why aren't you parsing the XML output? You could use DOMDocument or similar. If you have the option of receiving info via json, that would be even easier.

Jump to Post

All 8 Replies

You may want to store a delimiting character between each set of numbers. This way you can use the explode() function so you don't have to know exactly where one substring ends and another begins. For example, let's say you choose a comma , as your delimiter, your code is:

<?php
$output='01/07/2015,21/08/2016,91754.0'; // note the addded commas
$stringParts = array();
$stringParts = explode(',' $output);
// You can now access the parts at $stringParts[0], $stringParts[1], and $stringParts[2].
?>

thanks for quick reply

i had tried to print echo $var3 but it printing this encoding="iso-8859-1"?>

Member Avatar

Confused. You stated in the title...

how to add space between words

Yet the post just talks about a split (could be by substr or preg_split or str_split)

As you're talking about a regular length split, you can use a number of functions, but str_split is probably the easiest:

$output='01/07/201521/08/201691754.0';
list($v1,$v2,$v3) = str_split($output,10);

let me explain the issue, i am fetching the output from http API using curl and storing it into varibale $output

url: http://somesms.com/smscredit.jsp?user=UserName&password=Password

when you run this url directly into browser it displays the output in xml format like

<sms>

<accountexpdate>01/07/2015</accountexpdate>

<balanceexpdate>21/08/2016</balanceexpdate>

<credit>91754.0</credit>

</sms>
Member Avatar

So why aren't you parsing the XML output? You could use DOMDocument or similar. If you have the option of receiving info via json, that would be even easier.

Member Avatar
$xml = 
"<sms>
<accountexpdate>01/07/2015</accountexpdate>
<balanceexpdate>21/08/2016</balanceexpdate>
<credit>91754.0</credit>
</sms>";

$dom = new DOMDocument;
$dom->loadXML($xml);

$var1 = $dom->getElementsByTagName('accountexpdate')->item(0)->nodeValue;
$var2 = $dom->getElementsByTagName('balanceexpdate')->item(0)->nodeValue;
$var3 = $dom->getElementsByTagName('credit')->item(0)->nodeValue;

echo $var1 . ' ' . $var2 . ' ' . $var3;

thanks diafol, you are always my favorite on daniweb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.