/    Sign up×
Community /Pin to ProfileBookmark

Simple question

This is php 101 for sure, but for some reason I can’t find the answer. Not knowing how to word the search might be the problem actually…anyway, here you go.

I need to split strings by spaces.

So I have a form with drop downs to grab a month then a year. I then combine those values and write them to a single DB field. When I retreive them I would like to break them back out into seperate values. I guess I can do this with:

$date = explode(” “, $dates);
foreach($date as $datenum)
{
//alternate values with a counter for year and month
}

…but I seem to remember a built in method of just grabing the value of a string before or after a character (in my case a ” “).

Ugg someone hit me in the head with a shovel or something, why am I blanking on this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Brooksie155May 03.2006 — [code=php]$space_position = strpos($dates," ");[/code]

You should find that $space_position will contain the strings index value for the space.

Therefore:
[code=php]
$month = substr($dates,0,$space_position);
$year = substr($dates,$space_postion + 1, strlen($dates));
[/code]


Not sure if this is what you meant, personally I would stick with the way you were doing before except extract each part individually, as oppose to using a loop:

[code=php]
// Assuming "mm yyyy"
$date = explode(" ", $dates);
$month = $date[0];
$year = $date[2];
[/code]
Copy linkTweet thisAlerts:
@Doc_ThirstauthorMay 03.2006 — ARRGGG! Thanks man. Thats what Im looking for, seems as though I am getting my sql and my php mixxed up. I understand you can accomplish the same thing with a sql statment as well.

Sorry for brainfreeze. Thanks again.
Copy linkTweet thisAlerts:
@NogDogMay 03.2006 — [code=php]
$date = "May 2006";
list($month, $year) = explode(" ", $date);
[/code]
×

Success!

Help @Doc_Thirst 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.19,
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,
)...