Page 1 of 2

String search based on two seperators?

Posted: Mon Nov 28, 2005 9:48 am
by spacebiscuit
Hi,

Does anyone know if there is a function to split a string as follows.

Code: Select all

$string"DATA 1: testa DATA 2: testb DATA 3: testc";
I want to extract the strings testa, testb and testc.........

So I want to be able to somehow explode the string between two seperators [: and a space].

Many thanks,

Rob.

Posted: Mon Nov 28, 2005 9:50 am
by JayBird
Why not explode on the colon, then remove the whitespace from each result

although im pretty sure

Code: Select all

explode(': ', $string);
would work

Posted: Mon Nov 28, 2005 9:56 am
by spacebiscuit
What if the data (ie. testa) was a string with spaces?

Rob.

Re: String search based on two seperators?

Posted: Mon Nov 28, 2005 10:04 am
by foobar
robburne wrote:

Code: Select all

$string"DATA 1: testa DATA 2: testb DATA 3: testc";

Code: Select all

$parts = preg_split('/DATA[\d]+\:/', $string);

Posted: Mon Nov 28, 2005 10:23 am
by spacebiscuit
Hi could you explain how that function works as I am slightly puzzled. I have checked the manual but it makes no sense to me.

Thanks,

Rob.

Posted: Mon Nov 28, 2005 10:33 am
by spacebiscuit
A slight change, I need to split the string between the " : " symbol and the word DATA.....

Thanks,

Rob.

Posted: Mon Nov 28, 2005 10:41 am
by foobar
Slight change, I made a mistake in the Pattern Syntax (I didn't see the space after DATA):

Code: Select all

$parts = preg_split('/DATA[\s]+[\d]+:/', $string);
What this does, step by step:

1. Looks for an occurrence of the string 'DATA'.
2. Looks for one or more occurrences of a whitespace (symbolised by '\s', without quotes).
3. ... one or more occurrences of a digit ('\d').
4. ... followed by a colon.

I would recommend doing a search on Regex Coach on Google. It's a great program for debugging/developing regular expressions. It's freeware.

Posted: Mon Nov 28, 2005 10:54 am
by spacebiscuit
Ok thanks for the explanation that makes sense now........

However I am not sure if you have mis-readmy intentions. I need to search for data betweentwo seperators. So that on this string.....

$string"DATA 1: testa DATA 2: testb DATA 3: testc";

I am extracting testa, testb and testc.

I am guessing I want to search for data between a colon symbol and the word data?

Rob.

Posted: Mon Nov 28, 2005 11:01 am
by foobar
robburne wrote:Ok thanks for the explanation that makes sense now........

However I am not sure if you have mis-readmy intentions. I need to search for data betweentwo seperators. So that on this string.....

$string"DATA 1: testa DATA 2: testb DATA 3: testc";

I am extracting testa, testb and testc.

I am guessing I want to search for data between a colon symbol and the word data?

Rob.
That's exactly what the script does.

It returns everything between DATA{number}:.

If you do print_r($parts); you will see that $parts is an array like so: {'testa', 'testb', 'testc'}

If this isn't what you want, could you try explaining what you want instead?

Posted: Mon Nov 28, 2005 11:19 am
by spacebiscuit
Hi,

Using your exact code I amgetting no output.

Rob.

Posted: Mon Nov 28, 2005 11:48 am
by foobar
robburne wrote:Hi,

Using your exact code I amgetting no output.

Rob.
Do you mean...

Code: Select all

<?php

$string = 'DATA 1: testa DATA 2: testb DATA 3: testc';

$parts = preg_split('/DATA[\s]+[\d]+:/', $string);

print_r($parts);
?>
...this code?

Works fine for me.

Posted: Mon Nov 28, 2005 4:36 pm
by spacebiscuit
This is the text that I am searching, it is the body of an email message:

Code: Select all

echo"$message";

Code: Select all

This is a multi-part message in MIME format. ------=_NextPart_000_0170_01C5F46A.2EED1FD0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable POD TIME: 18:58 POD DATE: 18th Jan 2005 POD REF: 123456789 POD NAME: R Burne ------=_NextPart_000_0170_01C5F46A.2EED1FD0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable 
POD TIME: 18:58
POD DATE: 18th Jan 2005
POD REF: 123456789
POD NAME: R = Burne
------=_NextPart_000_0170_01C5F46A.2EED1FD0--
You can see the data that I am trying to extract on lines 6-9, the part after the colon sign.

I am using this to do so......

Code: Select all

$parts = preg_split('/DATA[\s]+[\d]+:/', $message);
However when I print the output I get:

Code: Select all

Array ( [0] => This is a multi-part message in MIME format. ------=_NextPart_000_0170_01C5F46A.2EED1FD0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable POD TIME: 18:58 POD DATE: 18th Jan 2005 POD REF: 123456789 POD NAME: R Burne ------=_NextPart_000_0170_01C5F46A.2EED1FD0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable 
POD TIME: 18:58
POD DATE: 18th Jan 2005
POD REF: 123456789
POD NAME: R = Burne
------=_NextPart_000_0170_01C5F46A.2EED1FD0-- )
Thanks,

Rob.

Posted: Mon Nov 28, 2005 5:04 pm
by foobar
Oh, I see. Well, you'd have to fiddle around with the Regular Expression then. I'll take a look at it tomorrow. :wink:

Posted: Tue Nov 29, 2005 7:47 am
by spacebiscuit
Foobar,

Hi I would appreciateit if you could take a look for me as I think I am misunderstanding the regex thing, take this simple example.....

Say I want to extract the letter B from "ABCDEF":

Code: Select all

$keywords = preg_split("/A+C/", "ABCDEF"); 
print_r($keywords);
I am getting :

Code: Select all

Array ( [0] => ABCDEF )
It is very frustrating as I can see what I need to be searching on/for but cannot get the syntax right and I can make neither head nor tail of the explantion in the php manual.

Any ideas?

Thanks,

Rob.

Posted: Tue Nov 29, 2005 9:30 am
by spacebiscuit
I've taken a look at the sticky tutorial on regex.....

here is my string.......

"POD DATE: 18.58 POD TIME 123456"

I want to extract the time part of the string "18.58"

So I am using..........

Code: Select all

$time=preg_split("/\d{2}.\d{2]/", $message);


That is..... any occurence of 2 digits followed by a fullstop followed again by 2 occurences of a digit.

Can it be any simpler than that can it?

My output is giving me.........

Array ( [0] => POD DATE: 18.58 POD TIME 123456 )

What am I doing wrong?

Rob.