473,856 Members | 1,481 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 1772
Your regular expression would probably look something like this:


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
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'


May 23 '06 #4

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

Similar topics

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
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
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();
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.
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"
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
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
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...
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"
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:
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...
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,...
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...
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...
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();...
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...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.