473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Preg_split question

Hi,

i have a problem with a regular expression.

i want to split a string on character pipe | with preg_split php function.

The string is :

"foo|bar|'hello|world\'abc\'def'|bye"

Result of split must be in an array :

foo
bar
hello|world'abc'def
bye


Anyone have an idea ?
Thank you !!
bye
Oct 6 '09 #1
14 4490
Markus
6,050 Expert 4TB
What is the expression you're using?
Oct 6 '09 #2
@Markus
i use preg_split function like this :

Expand|Select|Wrap|Line Numbers
  1. $val = "foo|bar|'hello|world\'abc\'def'|bye";
  2. $val = preg_split('/(?:\s\|\s)(?![\w\s]*\')/', $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  3. print_r($val);
But the pattern is wrong...
Oct 6 '09 #3
Markus
6,050 Expert 4TB
Hmm, the delimiter of your string is simply '|', that is, a pipe character? If so, explode() would be much better suited to it.

Expand|Select|Wrap|Line Numbers
  1. $string = "mark|jon|james|sarah|rachel|ben";
  2.  
  3. print_r(explode('|', $string));
  4.  
Oct 6 '09 #4
@Markus
No, watch my example :

"foo|bar|'hello|world\'abc\'def'|bye"

With explode function, i obtains :

foo
bar
'hello
world\'abc\'def'
|bye

I would like :

foo
bar
hello|world'abc'def
bye

It's not the same...
Thank you
Oct 6 '09 #5
TheServant
1,168 Expert 1GB
I can't imagine what your string is actually about. Why is hello|world'abc'def surrounded by inverted commas?? You would also like a pipe character | in one of the lines (not deliminated)?
I have a feeling that you will want to use regex functions rather than preg_split or explode(), and explicitly state the exact pattern...

It would be much easier to change your initial input to make the real deliminator something not included in one of your lines, or even a pattern like ||:
Expand|Select|Wrap|Line Numbers
  1. print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
Oh, and for the \'s in the exploded version, you might try stripslashes on it to get rid of them.
Oct 6 '09 #6
@TheServant
Your example is not good :

If my string is "foo|bar|'hello||world\'abc\'def'|bye" (note the ||)

your example code return :

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => foo
  4.     [1] => bar
  5.     [2] => 'hello
  6.     [3] => world\'abc\'def'
  7.     [4] => bye
  8. )
« 'hello||world\'abc\'def' » is splitted in 2 parts... not good

I have found a solution with preg :

Expand|Select|Wrap|Line Numbers
  1. $val = preg_split("/([a-zA-Z0-9]+:'.+?'|[^\|]+)\||$/", $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  2.  
Bye
Oct 6 '09 #7
TheServant
1,168 Expert 1GB
Glad you got it solved, but you did not read what I wrote. Between hello|world there is only one | while where you want a new line you use two ||.

You said: "foo|bar|'hello||world\'abc\'def'|bye"
I said: "foo||bar||'hello|world\'abc\'def'||bye"

Do you see the difference? I still think that as Markus suggested, it would be quicker and easier to use explode(), but as long as it works, I guess you're happy.
Oct 7 '09 #8
@TheServant
quicker and easier ??
ok, write me the function who parse input data...
Oct 7 '09 #9
there is a mistake in the preg_split solution, pattern is "/('.+?'|[^\|]+)\||$/"
Oct 7 '09 #10
TheServant
1,168 Expert 1GB
@pascalito
Are you controlling your deliminators or are they changing??
I wrote a code:
Expand|Select|Wrap|Line Numbers
  1. print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
  2.  
  3. /* Outputs: */
  4. Array 
  5.     [0] => foo 
  6.     [1] => bar 
  7.     [2] => 'hello|world\'abc\'def' 
  8.     [3] => bye 
  9. )
If you throw in a stripslashes:
Expand|Select|Wrap|Line Numbers
  1. print_r(explode("||",stripslashes("foo||bar||'hello|world\'abc\'def'||bye")));
  2.  
  3. /* Outputs: */
  4. Array 
  5.     [0] => foo 
  6.     [1] => bar 
  7.     [2] => 'hello|world'abc'def' 
  8.     [3] => bye 
  9. )
You have not shown us how your data is fetched/made, all you said was how do you turn "foo|bar|'hello|world\'abc\'def'|bye" into and array of:
foo
bar
hello|world'abc'def
bye
I suggested that if you change your deliminator from | to || then you can use explode while keeping your hello|world together.
If you deliminator is changing, you did not mention that. If you find preg_split easier, go for your life. Generally explode and similar string functions are easier for debugging.
Oct 7 '09 #11
TheServant, you do not answer the question...

I know how to use the explode function, everyone knows how to use this function...

you say it is quicker and easier to convert input data, ok, I ask you to write the function that converts input data from "foo|bar|'hello|world\'abc\'def'|bye" to "foo||bar||'hello|world\'abc\'def'||bye"

Regards,
Oct 7 '09 #12
I do not control the delimitator, otherwise, I never asked and I simplified input string...
Oct 7 '09 #13
TheServant
1,168 Expert 1GB
You never said that you couldn't control the deliminator, and how am I meant to know your experience? In that case, using regex (or preg_split) is the way you should go. Glad it's solved.
Oct 7 '09 #14
@TheServant
I never said any more than I could change the string but it seems logical to me.

If I could change the string, do you really think I asked any questions ...
Oct 7 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Peter | last post by:
L.S. I am trying to cut a string into substrings. The string at hand looks something like this: $data='one|two|three|four\nfive|six|seven|eight'; //observe the chr(10) in the middle I...
16
by: Sims | last post by:
Hi, I need some help to split data using regular expression Consider the string '1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly get =1, =2,=3. Now if i have
15
by: voipcanada | last post by:
is there any thing we can write to keep the right txt to the = sign in the preg_split. from the following lines DST-NUMBER-IN=0033512877596, DST-NUMBER-OUT=98751#33512877596,...
2
by: www.douglassdavis.com | last post by:
lets say I have a line of text with multiple spaces between each word.. I am not sure how many spaces. I would like to take the line of text, and change it to put only one space between the...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
3
by: none | last post by:
Hello: Using PHP 4.3.11 The line: $strDate = preg_split('//', $my_date); works to break apart a date in the form mm/dd/yyyy or mm.dd.yyyy or mm-dd-yyyy but the line: $strDate =...
0
by: tiggman | last post by:
Why preg_split works only if the char # before the delimiter is 21 or less? example php code: ============= $right = "{Hello Im very aaaaaa {cool|not cool}}"; print $right."<br>"; $pattern =...
1
by: skippychalmers | last post by:
Hey... new here. Could really use some help with a preg_split i'm trying to run. Basically, I have a string. In it is the tag: ''. Its a sort of bbcode feature I'm adding to a forum. There's...
3
by: mark1491 | last post by:
I am trying to split a string into sentences with preg_split, but I would like it to not split initials. Examples: Mark H. Doolittle is my name. What is yours. ( I don't want it to split the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.