String search based on two seperators?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

String search based on two seperators?

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

What if the data (ie. testa) was a string with spaces?

Rob.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: String search based on two seperators?

Post 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);
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

A slight change, I need to split the string between the " : " symbol and the word DATA.....

Thanks,

Rob.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Hi,

Using your exact code I amgetting no output.

Rob.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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:
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post 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.
Post Reply