Forum Moderators: coopster

Message Too Old, No Replies

Separating integers from characters

$variable = "123 successful"; //need to separating integers from characters

         

Mitch888

4:46 pm on Sep 3, 2003 (gmt 0)

10+ Year Member



$variable = "123 successful"; //need to separate integers from characters.

$variable = "123 successful";

How can I extract the ‘123’ from the value of $variable ‘123 successful’

Please help

Thank you

justageek

4:53 pm on Sep 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at preg_replace and do a pattern like [a-z]. That'll do it.

mikejson

5:47 pm on Sep 3, 2003 (gmt 0)

10+ Year Member



I'm going to give an answer that may or may not be do-able in php, but it should be. Use some type of string parser/tokenizer(something that makes a string into an array of words/chars, something like that), once you have the string put into ,for example, an array of words, then look through the words and find a string that is a number, easy to do using a traditional programming language. But I'm new to the php thing. I have heard that php is very powerful. Go to the docs and search for string manipulation tools.

In java, I could do this in about 5 mins, String tokenize, then search through and isInteger everything to find the numbers.

PHP is a good scripting language, and I'm almost positive Perl can do it, which means php should have some type of option to do it.

Good luck on finding it, let me know if you do

Asandir

8:56 pm on Sep 3, 2003 (gmt 0)

10+ Year Member



$newvar = preg_replace('/[^0-9]/',"","123 successful");

I think that should do it. It might need a little tweak.

claus

9:13 pm on Sep 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perl regexps has a way to do it, like, eg.

\d+

- which matches one or more digits. I'm just beginning to learn php, but perhaps it supports this shorthand as well.

/claus

vincevincevince

4:38 pm on Sep 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I extract the?23?from the value of $variable?23 successful?

Method one:


$variable="123 successful";
$variable=preg_replace("\[^0-9]\","",$variable);
echo $variable;

Method two:


$variable="123 successful";
preg_match("\[0-9]*\",$variable,$matches);
echo $matches[0];

Mitch888

5:49 pm on Sep 4, 2003 (gmt 0)

10+ Year Member



method one works like a charm. however, what is ("\[^0-9]\","",$variable)

the 0-9 is to look for integers, but what is the \ or the ^ or the []. how do you use them so I can understand
preg_replace() functions. I looked on the web and found lots of examples on how to use this function but none explained the special characters.

Thank you for your help guys

M.

coopster

7:35 pm on Sep 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You just have to know your resources, Mitch, and you already have one, WebmasterWorld! Here is another you should become quite familiar with php.net => Regular Expression Functions (Perl-Compatible) [us2.php.net]. Don't miss the link to this one, it becomes quite handy: Pattern Syntax [us2.php.net]. And, as always, use the forums when you get stuck ;)

Asandir

11:48 pm on Sep 4, 2003 (gmt 0)

10+ Year Member



Perl regexes are complex to learn, but bloody useful when you get the hang of it.

$newvar = preg_replace('/[^0-9]/',"","123 successful");

The first string is saying "find anything that *isn't* in the char range 0-9. The square brackets "[]" indicate that it should try and match any char in the brackets, and the caret "^" says to use the inverse of what is in the brackets.

Second string says replace it with an empty string. =)

Mitch888

12:55 am on Sep 5, 2003 (gmt 0)

10+ Year Member



php.net => Regular Expression Functions (Perl-Compatible) is really helpful and Asandir, you really clarified the function for me I thank you all. Now looking at it, it’s such a useful function and quite simple now.

Thanks again
M.