Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP preg_split by sentence

Posted on 2006-05-13
5
Medium Priority
?
1,113 Views
Last Modified: 2012-06-21
I have found an example of preg_split to split by sentence but I can't get it to work.

$Sentences=preg_split("/((?<=[a-z0-9)][.?!])|(?<=[a-z0-9][.?!]\"))(\s|\r\n)(?=\"?[A-Z])/",$scriptFile);

Now, my sentences in this case will all end in either period-quote-space OR questionmark-quote-space OR period-space OR questionmark-space OR exclaimationmark-space or exlamationpoint-quote-sapce

I don't get the above.  

What's the <=

explain:

?=\"?[A-Z]
0
Comment
Question by:cglenn3932
  • 4
5 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16673899
<= is part of the positive lookahead, and ?= is part of positive lookbehind.

I'll try and come up with a better regex for this (which will hopefully work as well) ;)

..
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16673940
This seems to work for me:

(?<=^|(\.|\?)"?)[^\.\?]*($|(\.|\?)"?)
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16673941
So:

$Sentences=preg_split("/(?<=^|(\.|\?)"?)[^\.\?]*($|(\.|\?)"?)/",$scriptFile);
0
 

Author Comment

by:cglenn3932
ID: 16675312
What is a positive lookahead, this Perl syntax is quite new to me.

I can't get yours to run.  I can get:

$Sentences=preg_split("/\./",$scriptFile);

to run, so the err must be ....?  I am running PHP 4.X, does that matter?
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 16677144
positive lookahead is a way of saying "get the text that precede's some other text, but don't include that other text". And positive lookbehind is the same thing, but for a string _after_ some other text, rather than before.

The regex I posted above works fine with my Java program which I tested it with; I've never really had any problems porting Java-compatible regex's to PHP before, but will have a play with it, and try and get something which is PHP-compatible..
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question