474,015 Members | 1,769 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
14 4553
TheServant
1,168 Recognized Expert Top Contributor
@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\'de f'|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
pascalito
9 New Member
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\'de f'|bye" to "foo||bar||'hel lo|world\'abc\' def'||bye"

Regards,
Oct 7 '09 #12
pascalito
9 New Member
I do not control the delimitator, otherwise, I never asked and I simplified input string...
Oct 7 '09 #13
TheServant
1,168 Recognized Expert Top Contributor
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
pascalito
9 New Member
@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
4362
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
3582
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
2189
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
3202
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
3114
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
2378
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
1155
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
2606
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
5195
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
10463
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
12015
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11542
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
11054
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
7768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6732
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5316
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
4869
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.