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

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

Post by foobar »

Take a look at your PCRE syntax again.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Sorry about the cross posting I did not know where I should be posting in php code or in regex.

What is PCRE?

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

Post by spacebiscuit »

\d ......Any single digit (0-9)

/\d{4}/ ......we can specify how many times a character should occur

Therefore I see no reason why this won't work....

Code: Select all

$time=preg_split("/\d{2}.\d{2]/", "18.58");
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

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

$values = preg_split("/DATA \d+\: /", $string);

array_shift($values);

print_r($values);

?>
EDIT: That'll teach me to not read the rest of the thread!!!1oneoneone
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

robburne wrote:What is PCRE?
Whaddya know... it's in the PHP Manual!

Thanks for taking the time to RTFM! :roll:
robburne wrote:

Code: Select all

$time=preg_split("/\d{2}.\d{2]/", "18.58");
I'm not going to let you off the hook that easily this time. Go through your code step by step. If you can't figure it out, get The Regex Coach and input the pattern. See what it tells you.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Thanks for the feedback, ok so it seems i have a typo in my expression . duh!

So with the change I am starting to get somewhere becuase.....

Code: Select all

$time=preg_split("/\d{2}.\d{2}/", "POD TIME: 18.00 ");
Is giving me...........

"POD TIME: "

So I think this show I understand the regex, just that I am not using the function correctly as I wantthe second half of the expression not the first.

Rob.
Last edited by spacebiscuit on Tue Nov 29, 2005 3:00 pm, edited 1 time in total.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

robburne wrote:Thanks for the feedback, ok so it seems i have a typo in my expression . duh!

So with the change I am starting to get somewhere becuase.....

Code: Select all

$time=preg_split("/\d{2}.\d{2]/", "POD TIME: 18.00 ");
Is giving me...........

"POD TIME: "

So I think this show I understand the regex, just that I am not using the function correctly as I wantthe second half of the expression not the first.

Rob.
Good. The function you'll want to use is preg_match_all(). What you are doing now, is splitting your string along each occurrence of two numbers separated by a dot. Using preg_match* you'll retrieve those numbers instead.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Thanks Foobar, I actually remember doing regular expressions in University about 5 years ago and although I found them interesting I never thought I would be using them in the big wide world!

I'll give the other function a try.

Thanks,

Rob.
Post Reply