Jump to content

Recommended Posts

I am checking for delimiter (.!?) and if there is a " after a sentence then it should neglect and start from next sentence

 

I am splitting into two sentences

"This is first sentence." This is second.

 

When I split it is taking the second sentence as

" This is second sentence. Which I dont want

 

Help please

Link to comment
https://forums.phpfreaks.com/topic/178545-chech-a-string-for-double-quotes/
Share on other sites

like this ?

<?php
$string = '"This is first sentence." This is second.
" This is second sentence. Which I dont want
"This is first sentence." Blar.
" This is second sentence. Which I dont want
';
preg_match_all('/".*?[.!?]"(.*?$)/sm', $string, $result, PREG_PATTERN_ORDER);
$result = $result[1];
var_dump($result);
?>

 

array(2) {

  [0]=>

  string(16) " This is second."

  [1]=>

  string(6) " Blar."

}

What is this code '/".*?[.!?]"(.*?$)/sm'

Its a regular expression

Also what does preg_match_all does

preg_match_all uses the regular expression to capture all of the results into an array

see manual preg_match_all

 

the regular expression i wrote does the following

matches a " then anything up until a . or ! or ? followed by a "

then captures everything to the end of the line

@MadTechie

 

Lets go over with the code...

 

$para = $_POST['para'];      //Getting para in variable

}

$sent = split('[.!?]',$para);      //Split based on .!?

 

foreach ($sent as $content)      //for all the arrays, sentences(sent)...do this

if (strlen($content)<=115)                    //some condition...

echo $content.'.<br />';

 

Now this code is resulting is few sentences starting with \"We need to eliminate this by checking sentences not starting with QUOTE

 

PLEASE HELP WITH CODE

 

It did not work for some reason.

Explain what you mean by "did not work"

 

Could you please explain me your regular expression in detail please

 

I did

the regular expression i wrote does the following

matches a " then anything up until a . or ! or ? followed by a "

then captures everything to the end of the line

 

What part do you need more detail on?

MadTechie this is your final warning.  If you cannot learn to be psychic I am demoting you.

 

This is a joke, right?

 

People here have seen how much help MadTechie has offered over the past few year.

 

The problem is that some people never understand or even bother to learn how to ask for help.

 

Coding wise, if you don't debug, or simply put, if you don't read your error messages and post back to the person who has been trying to help you with the returned errors.

 

What are you expecting?

 

I feel sorry for myself wasting my very virgin post...

Ahh okay..

 

It did not work for some reason.

I think the problem is a syntax error on one of the lines of code or maybe the logical error,

 

Could you please explain me your regular expression in detail please

 

 

regular expressions, also referred to as regex or regexp, provide a concise and flexible means for identifying strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

 

The following examples illustrate a few specifications that could be expressed in a regular expression:

 

    * the sequence of characters "car" in any context, such as "car", "cartoon", or "bicarbonate"

    * the word "car" when it appears as an isolated word

    * the word "car" when preceded by the word "blue" or "red"

    * a dollar sign immediately followed by one or more digits, and then optionally a period and exactly two more digits

 

Regular expressions can be much more complex than these examples.

 

Now

/".*?[.!?]"(.*?$)/sm

Matches the character " literally " then

Match any single character .*?

Between zero and unlimited times, as few times as possible, expanding as needed (lazy) *? then

Match a single character present in the list .!? [.!?] then

Match the character " literally " then

Match the regular expression below and capture its match into backreference number 1 (.*?$) then

Match any single character .*?

Between zero and unlimited times, as few times as possible, expanding as needed (lazy) *?

Assert position at the end of a line (at the end of the string or before a line break character) $

SM: dot matches newline; ^ and $ match at line breaks

 

Hope that helps :)

 

 

I feel sorry for myself wasting my very virgin post...

 

LMAO

MadTechie this is your final warning.  If you cannot learn to be psychic I am demoting you.

 

This is a joke, right?

 

No joke. Even though a lot of people can't be bothered with giving details about their problem or following advice, etc.. the customer is nonetheless always right.  Therefore we expect all staff members to be psychic, in order to cater to this.  That way, users not posting relevant stuff is no longer an issue.

MadTechie this is your final warning.  If you cannot learn to be psychic I am demoting you.

 

This is a joke, right?

 

No joke. Even though a lot of people can't be bothered with giving details about their problem or following advice, etc.. the customer is nonetheless always right.  Therefore we expect all staff members to be psychic, in order to cater to this.  That way, users not posting relevant stuff is no longer an issue.

 

in fact, there is an administration thread with good books about mastering psychic clairvoyance. i recommend "Psychic Reading in 30 Days" from O'Reillly. great read. best part is, if you've been paying attention, roughly halfway through you can already surmise what the rest of the book contains using the skills you've learned!

in fact, there is an administration thread with good books about mastering psychic clairvoyance. i recommend "Psychic Reading in 30 Days" from O'Reillly. great read. best part is, if you've been paying attention, roughly halfway through you can already surmise what the rest of the book contains using the skills you've learned!

The 2nd edition comes with a cheat sheet, I have read that i have no idea what 99% of it mean, I started reading it but you can't really skip anything, So I am still on the intro, about the how they used a super computer with MPR (multivariate pattern recognition) to map the regions of the brain, but I found it pretty dry,

ie

Researchers have used mathematical models to show that while our brains may be wired differently, a comparable pattern of neural activity and function can be observed between two brains when exposed to similar sensory input. Which may not seem like a big deal; after all, arriving at the same idea or solution as a colleague isn't a rare occurrence. But when you consider the sheer number of neurons and networks involved in the process, there would seem to be little chance for us to come up with the same answer at all. Even in a "network of 1,000 neurons, where each neuron can be connected to any other, there are a million possible contacts between any two neurons and consequently an unimaginably large number of possible networks," say researchers Raoul-Martin Memmesheimer and Marc Timme. "Each combination can have either an inhibiting or an activating effect on the downstream neuron and, in addition to this, can differ in its intensity and reaction time."

 

I mean, I can't even read minds while in a 10 meter radios (which is the easiest one),

 

akitchin, did you try or have any luck with CV's post about Advanced Jedi Mind Tricks is way to advanced for me..

 

Respected MadTechie

 

<?php

$string = "These are our sentences." This is great. "You are the best!" You can do it.;

 

preg_match_all('/" your expression/sm', $string, $result, PREG_PATTERN_ORDER);

....

foreach ($result as $result2){

echo $result2. '<br \>';

}

?>

 

my expected result:

These are our sentences

This is great

You are the best

You can do it

 

I hope you could help me with this now. I cannot explain my question more clearer than this!

 

 

That doesn't match your own logic,

 

but this should get the results you want

 

<?php
#$string = '"This is first sentence." This is second.'; //Match 2
#$string = '" This is second sentence. Which I dont want'; //Match 0
$string = '"These are our sentences." This is great. "You are the best!" You can do it.'; //Match 4

preg_match_all('/(?<=")\s*(.*?)[.!?]\s*(?="|$)/sm', $string, $result, PREG_PATTERN_ORDER);
$result = $result[1];
foreach ($result as $result2){
echo $result2. '<br \>';
}
?>

Thanks @MadTechie

 

Almost done !

 

this is my code

--------------

preg_match_all('/(?<=")\s*(.*?)[.!?]\s*(?="|$)/sm', $para, $result, PREG_PATTERN_ORDER);

$result = $result[1];

foreach ($result as $result2){

if (empty($result2))

    continue;

else { if (strlen($result2)<=115)

echo $result2.'.<br />';

else

echo substr($result2,0,115). '...Read More.<br />';

}

}

// Lets say I am accepting a big paragraph. But there are only 3 output sentences.

 

How can we change just enough to output all the sentences...??

 

If we give a big article for say. It will obviously have many sentences. I am splitting my article (paragraph) into sentences based on delimiters (.!?) By using the regular expression of MadTechie I am seeing only three sentences are formed in any large amount of paragraph I submit. I should get rest of the sentences till the paragraph finish right??

 

But the output is only 3 sentences using MadTechie's expression. Any solution??

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.