/    Sign up×
Community /Pin to ProfileBookmark

Parse string help

I have a string that includes first initial, last initial and number
I want to be able to parse out each initial and then the number into separate values?

Also how can I take a string containing a name and parse out just the first letter?

Please help. I am new to php.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriSep 05.2004 — you could do something like

[code=php]
$str = "R T 3";
$array = explode(" ", $str);
echo $array[0]; //First Initial
echo $array[1]; //Second Initial
echo $array[2]; //Number
[/code]


but if the string is always 5 characters in this format sometimes mite be R T 23

you can do
[code=php]
$str = "R T 3";
echo $str{0}; // First Initial
echo $str{3}; // Second Initial
echo $str{5}; //Number
[/code]


the second bit answers you second question too

[code=php]
$str = "Name";
echo ucfirst($str{0}) // Upper Case First Initial
[/code]
Copy linkTweet thisAlerts:
@estheraauthorSep 05.2004 — Thanks. as for the first part of the question -- I don't have spaces. It is exactly 2 letters and then a number (which could be anything from one to 400) How would I parse out the number?
Copy linkTweet thisAlerts:
@96turnerriSep 05.2004 — ok do this then

[code=php]
$str = "RT3";
$count = strlen($str);
echo $str{0}; // First Initial
echo $str{1}; // Second Initial
echo substr(2,$count-1); //Number
[/code]
×

Success!

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