[web] PHP String question

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

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.

anyone? :(

Share on other sites
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 on other sites
You could use strtok()...

$str1 = "123 gjf ahfd";$str2 = strtok($str1, " ");//$str2 is now equal to "123"

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 on other sites
oh cool...i think i'll be using the strtok() function instead though, seems simpler.

thanks guys!

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633352
• Total Posts
3011483
• Who's Online (See full list)

There are no registered users currently online

×