473,698 Members | 2,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3517
On 14 Jan 2006 12:39:27 -0800, in
<11************ **********@g14g 2000cwa.googleg roups.com>
(comp.lang.php) "zek2005" <es*******@gmai l.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*******@gmai l.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
4401
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 trigger4
7
3601
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
4
18566
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 (decimal points are not important). thnx
3
10141
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
1783
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 had apples(20) and pears(30)' how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)', how do I expect the '??'.
6
5955
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
2833
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 log files. The first file looks like: ...
0
2043
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
5
5758
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 extract the image of the document (just the first page if there are multiple pages) and save it as a TIFF so I can then use the Tesseract OCR to get the text in the image.
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.