473,837 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preg_split question

9 New Member
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\'de f'|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 4537
Markus
6,050 Recognized Expert Expert
What is the expression you're using?
Oct 6 '09 #2
pascalito
9 New Member
@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 Recognized Expert Expert
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
pascalito
9 New Member
@Markus
No, watch my example :

"foo|bar|'hello |world\'abc\'de f'|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 Recognized Expert Top Contributor
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
pascalito
9 New Member
@TheServant
Your example is not good :

If my string is "foo|bar|'hello ||world\'abc\'d ef'|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 Recognized Expert Top Contributor
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\'d ef'|bye"
I said: "foo||bar||'hel lo|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
pascalito
9 New Member
@TheServant
quicker and easier ??
ok, write me the function who parse input data...
Oct 7 '09 #9
pascalito
9 New Member
there is a mistake in the preg_split solution, pattern is "/('.+?'|[^\|]+)\||$/"
Oct 7 '09 #10

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

Similar topics

2
4351
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 would like to have this split two times. Once to give me the two 'sentences' 'one|two|three|four' and 'five|six|seven|eight'. Then, each of
16
3565
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
2170
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, DST-NUMBER-BILL=0033512877596 i only want 0033512877596, 98751#33512877596, 0033512877596
2
3195
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 words. So, my idea was to split it on the spaces, then implode it with one space between each word. But, this doesn't work: preg_split("/\\s*/","test test test test")
3
3098
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 before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
3
2369
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 = preg_split('//', $my_date); doesn't work.
0
1145
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 = '/({(?:+(?:"*"|\'*\')?)+})/'; $snip_array = preg_split($pattern, $right, -1, PREG_SPLIT_DELIM_CAPTURE); print_r($snip_array); ============ If I change $right = "{Hello Im very aaaaa {cool|not cool}}"; the
1
2597
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 one line of code that is key to the operation of the whole thing. It'll be within the tags, and should look like: run (anystring.app) I want to separate the string right after this line of code. So if the string was:
3
5184
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 middle initial, but want to split after 'name') The company is is called H.R. Block. They are a good company. ( I don't want it to split after 'H', 'R') Here is the code i have, but it causes a split at every period. can someone help me out, im...
0
9828
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9679
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10611
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4469
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.