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

How do you split a string using split()?

I have this PHP statement which works fine and breaks down a string of
lines delimited with a \r into an array.

$arrList=split("\r", $strList);

But what I really want is to be able to tell it to split out only the
strings that start with the letter "b".

E.g. I thought this would work "b\W\r"

I tried every regular expression but cant figure it out. I could use
some other function combinations to achive this but I though if there
is a way to do it with just spli()

Any ideas?

May 17 '06 #1
3 1725
Your regular expression would probably look something like this:

/^b(.+)\r/

For a line that begins with the letter 'b' is followed by one or more
'anything', ending with '\r'.

You would split the file into multiple lines with split(), then pass
each line to preg_match as $inputLine.

So you could use:

$numParts = preg_match($expression,$inputLine,$arrMatches);

Maybe theres a simpler way, I'm tired |-)

May 18 '06 #2
Carved in mystic runes upon the very living rock, the last words of ImOk
of comp.lang.php make plain:
I have this PHP statement which works fine and breaks down a string of
lines delimited with a \r into an array.

$arrList=split("\r", $strList);

But what I really want is to be able to tell it to split out only the
strings that start with the letter "b".

E.g. I thought this would work "b\W\r"

I tried every regular expression but cant figure it out. I could use
some other function combinations to achive this but I though if there
is a way to do it with just spli()


Since split() deletes the delimiter string that it's splitting on, I
don't think that function is going to give you what you want, regardless
of regex.

Try the preg_match_all() function.

An alternative would be to convert the appropriate \r characters to
something else unlikely to be in the string (such as a \3, for example),
and split on that:

$strList = preg_replace("/(b.+)\r/U", "\\1\3", $strList);
$arrList = explode("\3", $strList);

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 18 '06 #3
Based on your suggestions, I finally figured out a solution.

$strList="aabbcc\raaxxdd\rbbaacc\rbcxxx\rcaaxx\rcb xx\rabcdr\rbcd";
$arrList=split("\r", $strList);
$arrFiltered=preg_grep("/^b(.+)/",$arrList);

Split string into the array components and using preg_grep filter only
the elements that start with 'b'

Thanks

May 23 '06 #4

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
4
by: Henry Chen | last post by:
Hi, I have a string that needs to be parsed into the string. The separator is not char. It is something like " at ". With current string.Split function, it doesn't work. Is there any exist...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
4
by: Craig Buchanan | last post by:
I am trying to split a comma-delimited string into a string array. unfortunately, if the string doesn't contain a comma, the resulting array is Nothing. other than using vb6 compatibility, is...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.