By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

finding and extracting from a string

P: n/a
Hello

Heres the situation:
I got a file with lines like:

name:second_name:somenumber:otherinfo

etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
into array so that I can later operate on these strings.

And second, variation of it:
To get whole lines that contain some characters like 3 digits from a number
second b) :)
to extract only nmbers with some digits not whole lines.

Anyone ?
Please.

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 11 Oct 2003 00:07:39 +0200, "lecichy" <co*****@vline.pl> wrote:
Heres the situation:
I got a file with lines like:

name:second_name:somenumber:otherinfo

etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
into array so that I can later operate on these strings.
$array = explode(':', $line);

http://php.net/explode
And second, variation of it:
To get whole lines that contain some characters like 3 digits from a number
second b) :)
to extract only nmbers with some digits not whole lines.


Could you restate that more clearly; probably useful to show an example.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2

P: n/a

Uzytkownik "Andy Hassall" <an**@andyh.co.uk> napisal w wiadomosci
news:oo********************************@4ax.com...
On Sat, 11 Oct 2003 00:07:39 +0200, "lecichy" <co*****@vline.pl> wrote:
Heres the situation:
I got a file with lines like:

name:second_name:somenumber:otherinfo

etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn itinto array so that I can later operate on these strings.


$array = explode(':', $line);

http://php.net/explode
And second, variation of it:
To get whole lines that contain some characters like 3 digits from a numbersecond b) :)
to extract only nmbers with some digits not whole lines.


Could you restate that more clearly; probably useful to show an example.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)


explode's fine but what if text pieces like name, number, in the line are
not separated by colons only but different chars like:

"name:address;number\otherinfo"

i I want to get a list of numbers only so the string from field between ";"
and "\" . Generally, we could say that the problem is abaut extracting a
string from between specified chars. Like finding sentences with exclamation
mark in some text ( so string from between chars "." and "!" )

About the second
we got lines like:

John:Anderson;6305912\single
Anna:Smith;6302678\married
Mark:Smith;6540945\married

and i want to print all lines with Smith. (I input Smith and get second and
third line printed ) so what I mean here is to print a line with exact
match.
Second, to print whole number filelds (only ) witch partial match. So
whole number that starts with some digits. In otherwords I put "630" in the
form and i want to get 6302678, 6305912 printed.

Gosh.. I mixed it up. I barely understand what I want so feel free to give
up on me :)
Jul 17 '05 #3

P: n/a
On Sat, 11 Oct 2003 01:37:43 +0200, "lecichy" <co*****@vline.pl> wrote:
Uzytkownik "Andy Hassall" <an**@andyh.co.uk> napisal w wiadomosci
news:oo********************************@4ax.com.. .
On Sat, 11 Oct 2003 00:07:39 +0200, "lecichy" <co*****@vline.pl> wrote:
Heres the situation:
I got a file with lines like:

name:second_name:somenumber:otherinfo

etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn
it into array so that I can later operate on these strings.
$array = explode(':', $line);

http://php.net/explode


explode's fine but what if text pieces like name, number, in the line are
not separated by colons only but different chars like:

"name:address;number\otherinfo"

i I want to get a list of numbers only so the string from field between ";"
and "\" . Generally, we could say that the problem is abaut extracting a
string from between specified chars. Like finding sentences with exclamation
mark in some text ( so string from between chars "." and "!" )


Well, you've changed the problem now.

Looks like you're heading towards preg_split with a character class. But you
have to define what the separator characters are, of course.

$array = preg_split('/[:;\\]/', $line);
And second, variation of it:
To get whole lines that contain some characters like 3 digits from a
number second b) :)
to extract only nmbers with some digits not whole lines.


Could you restate that more clearly; probably useful to show an example.


About the second
we got lines like:

John:Anderson;6305912\single
Anna:Smith;6302678\married
Mark:Smith;6540945\married

and i want to print all lines with Smith. (I input Smith and get second and
third line printed ) so what I mean here is to print a line with exact
match.


preg_split as above, and then check for equality on the second element
returned ($array[1]).
Second, to print whole number filelds (only ) witch partial match. So
whole number that starts with some digits. In otherwords I put "630" in the
form and i want to get 6302678, 6305912 printed.
Partial leading match? Use substr to match the first n characters where n is
the length of your search string.
Gosh.. I mixed it up. I barely understand what I want so feel free to give
up on me :)


Hopefully the above gives you a nudge in the right direction at least.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #4

P: n/a
"lecichy" <co*****@vline.pl> wrote in message news:<bm**********@nemesis.news.tpi.pl>...
Hello

Heres the situation:
I got a file with lines like:

name:second_name:somenumber:otherinfo

etc with different values between colons ( just like passwd file)
What I want is to extract some part of it like all names or numbers from
each line, simply text fom between e.g. second and third colon. And turn it
into array so that I can later operate on these strings.

And second, variation of it:
To get whole lines that contain some characters like 3 digits from a number
second b) :)
to extract only nmbers with some digits not whole lines.

Anyone ?
Please.


http://in.php.net/preg_match
http://weitz.de/files/regex-coach.exe

---
"Silence is the only right answer for many wrong questions" --
G.K.Moopanaar, Indian Politician
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.