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
14 4490
What is the expression you're using?
@Markus
i use preg_split function like this : - $val = "foo|bar|'hello|world\'abc\'def'|bye";
-
$val = preg_split('/(?:\s\|\s)(?![\w\s]*\')/', $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
-
print_r($val);
But the pattern is wrong...
Hmm, the delimiter of your string is simply '|', that is, a pipe character? If so, explode() would be much better suited to it. -
$string = "mark|jon|james|sarah|rachel|ben";
-
-
print_r(explode('|', $string));
-
@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
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 ||: - 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.
@TheServant
Your example is not good :
If my string is "foo|bar|'hello||world\'abc\'def'|bye" (note the ||)
your example code return : - Array
-
(
-
[0] => foo
-
[1] => bar
-
[2] => 'hello
-
[3] => world\'abc\'def'
-
[4] => bye
-
)
« 'hello||world\'abc\'def' » is splitted in 2 parts... not good
I have found a solution with preg : -
$val = preg_split("/([a-zA-Z0-9]+:'.+?'|[^\|]+)\||$/", $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
-
Bye
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.
@TheServant
quicker and easier ??
ok, write me the function who parse input data...
there is a mistake in the preg_split solution, pattern is "/('.+?'|[^\|]+)\||$/"
@pascalito
Are you controlling your deliminators or are they changing??
I wrote a code: - print_r(explode("||","foo||bar||'hello|world\'abc\'def'||bye"));
-
-
/* Outputs: */
-
Array
-
(
-
[0] => foo
-
[1] => bar
-
[2] => 'hello|world\'abc\'def'
-
[3] => bye
-
)
If you throw in a stripslashes: - print_r(explode("||",stripslashes("foo||bar||'hello|world\'abc\'def'||bye")));
-
-
/* Outputs: */
-
Array
-
(
-
[0] => foo
-
[1] => bar
-
[2] => 'hello|world'abc'def'
-
[3] => bye
-
)
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.
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,
I do not control the delimitator, otherwise, I never asked and I simplified input string...
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.
@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 ...
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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,...
|
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...
|
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...
|
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 =...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |