Telling apart full stop & decimal point
Moderator: General Moderators
Telling apart full stop & decimal point
How can I check if a dot is in between two numbers in a string?
Re: Telling apart full stop & decimal point
Regular expression matching [0-9]\.[0-9]?
Re: Telling apart full stop & decimal point
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]?
How would I put this into code: split a string into sentences where it doesn't match [0-9]\.[0-9]?
Re: Telling apart full stop & decimal point
Something like this should mostly do the trick.
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.
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 />";
}Re: Telling apart full stop & decimal point
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:
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);Re: Telling apart full stop & decimal point
What is the aim of that?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.
Re: Telling apart full stop & decimal point
To split a string into an array of sentences, I would do this.
The pattern finds one or more whitespace characters that are preceded by a sentence-ending punctuation character.
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.
)
*/Re: Telling apart full stop & decimal point
Thanks, the only problem with that is if there is a human error and they don't add a space.lol.
Re: Telling apart full stop & decimal point
You shouldn't be expected to turn rubbish into rubies.
Re: Telling apart full stop & decimal point
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?
However the code I have replaces the two numbers as well, I only need it to replace the decimal point.
Any ideas?
Re: Telling apart full stop & decimal point
Use regex to match it, use the returned matches array to find its position in the string, then replace?
Re: Telling apart full stop & decimal point
Two solutions... but don't say I didn't warn you.*lenton wrote:code that replaces '.' with a ',' where matches /[0-9]\.[0-9]/
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."
*/