474,015 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split into sentences with preg_split

2 New Member
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 5194
ronverdonk
4,258 Recognized Expert Specialist
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 Recognized Expert Expert
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
mark1491
2 New Member
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
1625
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
2603
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 split with a regex built up of the delimiters separated by "|", but it doesn't return the delimiters which I need. The goal is an algorithm that can take a string containing html code using <ul>, <ol>, and <li> and turn it into a formated...
2
3289
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 the space character (or tab or newline). ex... $tklist = preg_split("/\s|\n|\t/", $str); This works except in the string case, where "HELLO WORLD!" should not be two parts. How do I split by spaces but keep phrases inside quotes
3
2378
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 = preg_split('//', $my_date); doesn't work.
2
3327
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 expression for this... Anyone any idea please? Thanks, Chris.
1
2065
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 strings for the first field in both queries.
3
1492
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 searching the documentation without success. TIA Vince
3
2516
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 -elephant/tiger"; would give
0
1657
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 to be sent as individual SMS. I am posting the function here in case its useful for someone. Regards
0
10462
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, 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...
0
10282
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
12013
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
11541
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
11053
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
10217
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...
1
8604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6571
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...
1
5315
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 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.