473,406 Members | 2,707 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,406 software developers and data experts.

split into sentences with preg_split

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 not good a regex yet.

[PHP]$sentences = preg_split ("/[.?!]+/", $data);[/PHP]
Apr 4 '08 #1
3 5160
ronverdonk
4,258 Expert 4TB
That is quite difficult to distinguish. Example: what would such an expresssion do with sentences like
Expand|Select|Wrap|Line Numbers
  1. That is for you and I. But not for him.
No expression can distinguish between you "Mark H. Doolittle" and "and I. But".

Ronald
Apr 6 '08 #2
Atli
5,058 Expert 4TB
I cant see a simple solution to this either.

For this to work, the code would have to be able to distinguish between the end of line periods and those used for other purposes, such as names and numbers.

Your code would have to be able to recognize all possible uses of a period and decide whether or not it really is the end of a line.
Honestly, I doubt that is "doable"... (I would say possible, but nothing is "impossible" ;P)
Apr 6 '08 #3
That is quite difficult to distinguish. Example: what would such an expresssion do with sentences like
Expand|Select|Wrap|Line Numbers
  1. That is for you and I. But not for him.
No expression can distinguish between you "Mark H. Doolittle" and "and I. But".

Ronald
I understand that there will definitely be some problems, but most likely the only one letter word that would fall at the end of a sentence would be "I" like in your example:

Expand|Select|Wrap|Line Numbers
  1. That is for you and I. But not for him.
I'll figure that out later, but for now is there anyway you could show me how i would do a preg_split that would:

not split any "." that came after a single character instance of 'a-zA-Z', but then split the rest of the "." that did not follow that rule
Apr 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alexander Ross | last post by:
is there an easy way to split a string on SPACE except if that SPACE is within QUOTES? i.e. - $str = red green blue yellow "blue green" returns ?
3
by: Justin L. Kennedy | last post by:
I am looking for a function that takes in a string and splits it using a list of other strings (delimiters) and can return the delimiters as well as the extra parts of the string. I was trying the...
2
by: afrinspray | last post by:
I'm writing a function that parses a nested list string that might look like this: ( "HELLO WORLD!" 1231231 awesome ( 1 2 ) ) I wrote the logic already and it starts by splitting the string by...
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 =...
2
by: Chris Thompson | last post by:
Hi All, Trying to split a string where there are occurances of a space, a comma, a newline (char 10), or a return (char 13) using the preg_split function, but not sure how to write the regular...
1
by: Ben | last post by:
Hi, To simplify this request, we supposed this following sql script: "insert into table a values ('me;you','then'); insert into table a values ('you;me','so');" Note the semi-colon ( ; ) in...
3
by: Vince Morgan | last post by:
My problem has a simple solution. However I just can't find it. I cannot remember which 'split()' type function allows you to keep the delimiter in place within the final array. I've tried...
3
by: Geoff Berrow | last post by:
Does anyone know what regular expression I would use to split text into an array, assuming the text can be separated by any non alphanumeric character? e.g $string ="cat,dog fish, mouse ...
0
by: John | last post by:
Hi I have written a function to split a string into sub strings of a given fixed max length. This is useful for example in breaking a long message into multiple strings of up to 160 characters...
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?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.