Telling apart full stop & decimal point

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Telling apart full stop & decimal point

Post by lenton »

How can I check if a dot is in between two numbers in a string?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Telling apart full stop & decimal point

Post by Celauran »

Regular expression matching [0-9]\.[0-9]?
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Telling apart full stop & decimal point

Post by lenton »

I don't really know a lot about preg.

How would I put this into code: split a string into sentences where it doesn't match [0-9]\.[0-9]?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Telling apart full stop & decimal point

Post by Celauran »

Something like this should mostly do the trick.

Code: Select all

$instances =  preg_match_all('/[^0-9]\./', $string, $matches);

$sentences = array();
for ($i = 0; $i < $instances; $i++)
{
    $sentences[$i] = substr($string, 0, stripos($string, $matches[0][$i]) + 2);
    $string = substr($string, strlen($sentences[$i]));
}

foreach ($sentences as $sentence)
{
    echo $sentence . "<br />";
}
It still needs a little tweaking, though. For instance, you'll run into trouble when you have sentences that end with numbers or sentences that consist only of numbers.
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Telling apart full stop & decimal point

Post by lenton »

I can't seem to get it working. I could change the decimal into a comer then change it back once the sentences have been split.
This is what I have but it replaces the numbers as well as the decimal:

Code: Select all

$input = preg_replace("/[0-9]\.[0-9]/", ",", $input);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Telling apart full stop & decimal point

Post by Celauran »

lenton wrote:I can't seem to get it working. I could change the decimal into a comer then change it back once the sentences have been split.
What is the aim of that?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Telling apart full stop & decimal point

Post by McInfo »

To split a string into an array of sentences, I would do this.

Code: Select all

$str = "My current ranking is 408. Pi is close to 3.14.
I have 13.25 chickens. Yikes! Where did the .25 come from?
Did you know 123.nobody@nowhere.not is not a real email address?
My IP address is 127.0.0.1. I bet yours is too.";

print_r(preg_split('/(?<=[.!?])\s+/', $str));
/*
Array
(
    [0] => My current ranking is 408.
    [1] => Pi is close to 3.14.
    [2] => I have 13.25 chickens.
    [3] => Yikes!
    [4] => Where did the .25 come from?
    [5] => Did you know 123.nobody@nowhere.not is not a real email address?
    [6] => My IP address is 127.0.0.1.
    [7] => I bet yours is too.
)
*/
The pattern finds one or more whitespace characters that are preceded by a sentence-ending punctuation character.
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Telling apart full stop & decimal point

Post by lenton »

Thanks, the only problem with that is if there is a human error and they don't add a space.lol.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Telling apart full stop & decimal point

Post by McInfo »

You shouldn't be expected to turn rubbish into rubies.
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Telling apart full stop & decimal point

Post by lenton »

All I need is code that replaces '.' with a ',' where matches /[0-9]\.[0-9]/

However the code I have replaces the two numbers as well, I only need it to replace the decimal point.

Any ideas?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Telling apart full stop & decimal point

Post by Celauran »

Use regex to match it, use the returned matches array to find its position in the string, then replace?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Telling apart full stop & decimal point

Post by McInfo »

lenton wrote:code that replaces '.' with a ',' where matches /[0-9]\.[0-9]/
Two solutions... but don't say I didn't warn you.*

Code: Select all

$subject = 'I have 4.5 ideas. There are 5,280* feet
in 1 mile.6 is a typo. Pi is near 3.14.';

// Lookbehind and lookahead assertions:
var_dump(preg_replace('/(?<=\d)\.(?=\d)/', ',', $subject));
/*
string(78) "I have 4,5 ideas. There are 5,280* feet
in 1 mile.6 is a typo. Pi is near 3,14."
*/

// Back references to captured subpatterns:
var_dump(preg_replace('/(\d)\.(\d)/', '\1,\2', $subject));
/*
string(78) "I have 4,5 ideas. There are 5,280* feet
in 1 mile.6 is a typo. Pi is near 3,14."
*/
Post Reply