473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Extract numbers from string

Hi friends!

I have a varchar field in my DB with numeric values separates by
spaces. I need to extract the numbers to create an array.

Example 1: 1820 1823 1825 --> need to be transform into

1820
1823
1825

Example 2: 1 5 21 31 <<> must be transform into
1
5
21
31

The difference between ex 1 and 2 is that there might be different
length between numbers.

Can anyone help me???

Thank you very much,

Ezequiel

Jan 14 '06 #1
3 3500
On 14 Jan 2006 12:39:27 -0800, in
<11**********************@g14g2000cwa.googlegroups .com>
(comp.lang.php) "zek2005" <es*******@gmail.com> wrote:
Hi friends!

I have a varchar field in my DB with numeric values separates by
spaces. I need to extract the numbers to create an array.

Example 1: 1820 1823 1825 --> need to be transform into

1820
1823
1825

Example 2: 1 5 21 31 <<> must be transform into
1
5
21
31

The difference between ex 1 and 2 is that there might be different
length between numbers.

Can anyone help me???


$myArray = explode(" ", $myString)

explode() splits up the string in its second argument at each
ocurrence of the string in its first argument, and returns the results
into an array.

The manual http://fr.php.net/manual/en/function.explode.php
gives good examples.

Cheers

--
Charlie
Jan 14 '06 #2
zek2005 said the following on 14/01/2006 20:39:

I have a varchar field in my DB with numeric values separates by
spaces.
Well this is generally a bad idea to start with - one of the principles
of good database design is atomicity, i.e. each field should contain
only one value.

i.e. instead of something like:

ID name values(VARCHAR)
================================
1 Alice 1 28 373
2 Bob 72 182 44

you should use two tables:

ID name
============
1 Alice
2 Bob

personID value(INT)
=====================
1 1
1 28
1 373
2 72
2 182
2 44
Not to mention that storing numeric values in a character-based data
type is a waste of space and processing time.

I need to extract the numbers to create an array.


But if you must store your info this way, use explode() to extract the
individual values.
--
Oli
Jan 14 '06 #3
Charlie King wrote:
(comp.lang.php) "zek2005" <es*******@gmail.com> wrote:
I have a varchar field in my DB with numeric values separates by
spaces. I need to extract the numbers to create an array.
Example 1: 1820 1823 1825 --> need to be transform into
1820
1823
1825


$myArray = explode(" ", $myString)

explode() splits up the string in its second argument at each
ocurrence of the string in its first argument, and returns the results
into an array.

The manual http://fr.php.net/manual/en/function.explode.php
gives good examples.

Cheers


Exactly - I was going to say the same thing if no one else had yet :)
Jan 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lydia Shawn | last post by:
hi there, i want to extract the numbers from this example input: bla trigger3 trigger4 trigger1 blabla trigger1 5000.00 trigger3 trigger1 trigger2 trigger2 600.00 trigger4 trigger1 50.00...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
4
by: Ryan | last post by:
I am trying to write a c# function to extract all numbers out of a string. What is the easiest way to do this, regular expressions? I must account for numbers formatted with or without commas...
3
by: Jay Douglas | last post by:
Hello all. I'm having a hard time coming up with a regular expression that can extract just the numbers out of string into a GroupCollection. Any help is appreciated.
3
by: David Moore | last post by:
Hi All, I expect someone can crack this one in no time. Ok, what I'd like to do is to match a pattern in a string and extract a portion of it. For example if I had a string like: 'The store...
6
by: Dave | last post by:
Hope someone can help! I have a memo fiels in which there are a few numbers including dates but what I want to do is extract a number which is 6 figures long. Can anyone help me? Thanks Dave
8
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.