2

I have a pipe-delimited dump file from an SQL Server, and I want to import it into MySQL. The lines are delimited by \r\n, and that sequence also occurs in some fields! So I want to use a regular expression to find the actual lines and make an INSERT statement out of them.

However, I'm having trouble including the delimiter in my match string. I thought using PREG_SPLIT_DELIM_CAPTURE would do the trick but apparently I'm doing something wrong. My delimiter is three spaces followed by three numbers, which is actually the id that I need for the row:

$ cat test.php
<?
$string = '   897|a|Hello\r\n   583|b|Line\r\nBreak\r\n   332|c|Yet\r\nMore\r\nLine\r\nBreaks\r\n';

$lines = preg_split( '/   \d{3}\|/', $string, NULL, PREG_SPLIT_DELIM_CAPTURE);
print_r($lines);

$ php test.php
Array
(
    [0] => 
    [1] => a|Hello\r\n
    [2] => b|Line\r\nBreak\r\n
    [3] => c|Yet\r\nMore\r\nLine\r\nBreaks\r\n
)

My delimiters are missing.

$ php -v
PHP 5.3.3-7+squeeze1 with Suhosin-Patch (cli) (built: Mar 18 2011 17:22:52) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

What am I doing wrong, or how do I get what I want?

2 Answers 2

8

You need to group your delimiter into parenthesis, or else the _DELIM_CAPTURE will have no effect.

$lines = preg_split( '/   (\d{3}\|)/', $string, NULL, PREG_SPLIT_DELIM_CAPTURE);

Here, the manual mentions it en passant as flag description:

PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.

Sign up to request clarification or add additional context in comments.

3 Comments

I had read that, but I didn't understand what parenthesized meant, although its fairly obvious now :P
OK, that gives me an array with the delimiter being one element and the shard being the next... is there any way to get them as part of the same element? Assertions or something like that?
No, preg_split will only give them out separated. You would have to construct a preg_match_all pattern if you wanted them combined.
1

Above answer will give you delimiter in separate array element.

Check this one instead: preg_split how to include the split delimiter in results?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.