/    Sign up×
Community /Pin to ProfileBookmark

Spliting a string into array

[URL=http://www.php.net]php.net[/URL]‘s documentation for explode, split, and other functions is a little, well, wierd.

Can someone point me in the right direction?
I am looking for a script that will split a string into an array.

[code=php]<?php
$variable = ’12/34/2000′;
$variable = explode(‘/’,$variable,3);
echo $variable[0];//somthing like “12”
?>[/code]

For some reason, I get an error with [COLOR=firebrick]explode()[/COLOR] AND [COLOR=firebrick]split()[/COLOR]:
[COLOR=firebrick]Notice: Undefined offset: 1 in …/script.php on line 148[/COLOR]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@fyrestrtrOct 05.2003 — Your problem is that when you are setting the optional limit, it is too big for the array.

<i>
</i>For an input like 01/10/2000
[0] = 01
[1] = 10
[2] = 2000


As you can see, there is no element at index [3].

You don't need to set the limit, instead, you can do this :

[code=php]
$var = "00/00/0000";
$parts = explode("/",$var);
echo "<pre>"; print_r($parts); echo"</pre>";
[/code]


This will show you what the array is that is being returned. From there, you can pick out the index of the element you want.
Copy linkTweet thisAlerts:
@diamondsauthorOct 05.2003 — That dosen't seem to be the problem...


[code=php]
<?php
$q = "value1>value2";
$tmp_arr_1 = split(">",$q,1);
$program = $tmp_arr_1[0];
echo $tmp_arr_1[0];
?>
[/code]


returns:

"value1>value2"

and there is no value

[COLOR=darkred]$tmp_arr_1[1][/COLOR].

and it dosen't matter if I change it to [COLOR=darkred]$tmp_arr_1 = split(">",$q);[/COLOR], removing the "[COLOR=darkred],1[/COLOR]" from the near-end of the function
Copy linkTweet thisAlerts:
@diamondsauthorOct 05.2003 — It does help, removing the limit area.

But, how do I make it split only at the first separator, and never again? without an error?
Copy linkTweet thisAlerts:
@diamondsauthorOct 05.2003 — By doing somthing like this, I can control the errors:
[code=php]if(substr_count(var,'/')<=2){
$var2 = explode('/',$var);
}else{
$var2 = explode('/',$var,1);
}[/code]


thanks ?
Copy linkTweet thisAlerts:
@fyrestrtrOct 06.2003 — If you just want to split it at one character, then you don't need split() or explode(). You can do it with a combination of string functions.

Say you wanted everything to the [i]right[/i] of a certain character. You can then use [url=http://www.php.net/strstr]strstr[/url], which will return everything from a certain needle (or search point), till the end of the string. The returned string will include the needle.

Example (from the manual) :

[code=php]
$email = '[email protected]';
$domain = strstr($email, '@');
print $domain; // prints @example.com
[/code]


If you want to return everything to the [i]left[/i] of a string (everything leading up to the needle), then you can use a combination of [url=http://www.php.net/substr]substr[/url] and [url=http://www.php.net/strpos]strpos[/url].

Example :

[code=php]
$haystack = "10/10/1000";
$needle = "/";
echo substr($haystack,0,strpos($haystack,$needle)+1);
[/code]


Hope this helps.
×

Success!

Help @diamonds spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...