Jump to content
  • Advertisement
Sign in to follow this  
_nomad_

[web] PHP String question

This topic is 4431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

alright, noob asking :( I have this: $someString = "23 testingWorld hi" and I wish to get only "23" in that string, and put it to $anotherString. how do i parse the first integer in that string in PHP? it could be "553 somestring hi", meaning more than two digits. thanks.

Share this post


Link to post
Share on other sites
Advertisement
for that case just doing

$anotherString = (int)$someString

should work

for others you could use the split function to chop the string up into peices if you know what's going to come after the number and use the result of that function [0] to get the number

and even further on you could use the regex functions to validate and extract the number part (I'm to lazy to remember or look up how to do this one)

Share this post


Link to post
Share on other sites
You can use PHP's explode to split a string into an array. The "opposite" is implode, which creates a string from an array.

-Riku

Share this post


Link to post
Share on other sites
oh cool...i think i'll be using the strtok() function instead though, seems simpler.

thanks guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!