473,385 Members | 1,311 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,385 software developers and data experts.

preg_split problem

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
these sentences would be split into their repective words. Initially I
thought it would be simple:

$sentence=preg_split("\n",$data); //should give me an array $sentence
$word=preg_split("|",$sentence[0]); //should give me an array $word

Unfortunately this doesn't work and now I am caught up in a new world of
'regular expression functions' that doesn't seem to make any sense. Can you
please help me to get this split function to do what I need?

Thanks for your reply, Peter.
Jul 16 '05 #1
2 4317
YES!!! That appears to do the trick. Thank you.

Peter.
Jul 16 '05 #2
Peter wrote:
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
these sentences would be split into their repective words. Initially I
thought it would be simple:

$sentence=preg_split("\n",$data); //should give me an array $sentence
$word=preg_split("|",$sentence[0]); //should give me an array $word


As pointed out elsewhere, it is better to use explode.

However, if you are doing things like this with pcre, you have to bear in mind
that if your expressions contain newlines, you have to use a multiline modifier.
Have a read and a play with it (read the manual stuff on modifiers)

Matt
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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
117
by: Peter Olcott | last post by:
www.halting-problem.com
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: 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...
14
by: pascalito | last post by:
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"
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.