Preg_split help needed..

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

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Preg_split help needed..

Post by kaisellgren »

Hi,

Code: Select all

$data = "this is a test string. This is another test. And another. But \. this hope fully does not get split up. While \\. this should be ;)";
$split = preg_split("#\\.#",$data);
I want to split the text into parts using the dot. I don't need the dot to be a part in the splitted content. Actually I want the dot be gone. This is all easy, but when it comes to taken those backslashes into consideration, it's harder...

The "\." should not be counted to be splitten... Doing something like

Code: Select all

$split = preg_split("#[^\\]\\.#",$data);
Will work OK, but the problem here is that the preg_split will take the character near the dot away and it does not exist in the splitted strings...

Also "text. \\. test" should be 2 parts... so I need a completely working splitting. The escaping can be anything, it might be "\\\\\\\\\\." and it must be handled correctly, MUST be...

Any help?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Preg_split help needed..

Post by John Cartwright »

Code: Select all

$result = preg_split('#\.(?=\\)#', $subject, 2);
It's been quite some time since I've used regex, but what you are looking for is called a negative lookahead. Assuming that example doesn't work (untested), that page should sort you out.

Good luck.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Preg_split help needed..

Post by kaisellgren »

Jcart wrote:

Code: Select all

$result = preg_split('#\.(?=\\)#', $subject, 2);
It's been quite some time since I've used regex, but what you are looking for is called a negative lookahead. Assuming that example doesn't work (untested), that page should sort you out.

Good luck.
Yeah that works pretty well, but there are situations where it does not work. Like "this should not be splitted \\\. in half" while "this should be splitted \\. in half".
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Preg_split help needed..

Post by John Cartwright »

If I'm not mistaken, it should not match any period that is preceeded by a backslash. Sorry I'm rusty.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Preg_split help needed..

Post by kaisellgren »

Jcart wrote:If I'm not mistaken, it should not match any period that is preceeded by a backslash. Sorry I'm rusty.
Yes. I actually solved this, it's rather Hacky method.. I'm looping with For and changing $esc = true/false etc... but it's fine for me use ...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Preg_split help needed..

Post by John Cartwright »

Did you try my method? It should do exactly what I just said..
Post Reply