473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1759
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($exp ression,$inputL ine,$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="aabbc c\raaxxdd\rbbaa cc\rbcxxx\rcaax x\rcbxx\rabcdr\ rbcd";
$arrList=split( "\r", $strList);
$arrFiltered=pr eg_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
31177
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 will split myString up using the delimiter of 1 space so that
4
2580
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 functions like "Split" that I can use to do the job? Any idea of the simpliest way to do the job? Thanks in advance, Henry
5
57198
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 could get only 1st line, but it is not returning 2nd line. here's code: string address = null; string ssep = Environment.NewLine; char sep = ssep.ToCharArray();
19
10920
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 empty strings in the resulting string array.
4
5785
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
6887
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
1324
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 there another option? thanks, Craig Buchanan
3
9667
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... field1...field2...field3...
7
2259
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 getting the results I expect. Dim SA as string() Dim S as string S="FBE"
4
6045
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 the doc I see:
0
8739
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,...
0
9384
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
9238
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
9088
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
8052
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...
0
5995
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
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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.