Welcome to WebmasterWorld Guest from 35.175.120.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Separating integers from characters

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

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

Junior Member

10+ Year Member

joined:Feb 8, 2003
posts:61
votes: 0


$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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 21, 2003
posts:1069
votes: 0


Look at preg_replace and do a pattern like [a-z]. That'll do it.
5:47 pm on Sept 3, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 26, 2003
posts:141
votes: 0


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

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

Junior Member

10+ Year Member

joined:Oct 8, 2002
posts:65
votes: 0


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

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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 15, 2003
posts:2412
votes: 5


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

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

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


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];
5:49 pm on Sept 4, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 8, 2003
posts:61
votes: 0


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.

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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 ;)
11:48 pm on Sept 4, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 8, 2002
posts:65
votes: 0


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. =)

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

Junior Member

10+ Year Member

joined:Feb 8, 2003
posts:61
votes: 0


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.