Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
retypos-adapter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
eterfund
retypos-adapter
Commits
4c886e20
Commit
4c886e20
authored
May 03, 2019
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed patterns of typo, corrected and context. Added abstract function clearText()
parent
418bb4e1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
10 deletions
+26
-10
TyposClientInterface.php
src/TyposClientInterface.php
+26
-10
No files found.
src/TyposClientInterface.php
View file @
4c886e20
...
@@ -2,8 +2,6 @@
...
@@ -2,8 +2,6 @@
/**
/**
* Created by PhpStorm.
* Created by PhpStorm.
* User: ambulance
* User: ambulance
* Date: 25.05.18
* Time: 19:39
*/
*/
namespace
Etersoft\Typos
;
namespace
Etersoft\Typos
;
...
@@ -210,7 +208,7 @@ abstract class TyposClientInterface
...
@@ -210,7 +208,7 @@ abstract class TyposClientInterface
* @param string $typo
* @param string $typo
* @param string $corrected
* @param string $corrected
* @param string $context
* @param string $context
* @param $text
* @param
string
$text
*
*
* @return string Text with typo replaced by corrected
* @return string Text with typo replaced by corrected
* @throws \Exception If something goes wrong
* @throws \Exception If something goes wrong
...
@@ -219,10 +217,8 @@ abstract class TyposClientInterface
...
@@ -219,10 +217,8 @@ abstract class TyposClientInterface
"Context not found", 405
"Context not found", 405
*/
*/
private
function
replaceTypoInText
(
string
$typo
,
string
$corrected
,
string
$context
,
string
$text
)
{
private
function
replaceTypoInText
(
string
$typo
,
string
$corrected
,
string
$context
,
string
$text
)
{
// Strip all tags from text
// Copy input string
// Also copy input string
$originalText
=
$text
;
$originalText
=
$text
;
$text
=
strip_tags
(
$text
);
/*
/*
// BUG# 13121
// BUG# 13121
$typo = str_replace("\xc2\xa0", " ", $typo);
$typo = str_replace("\xc2\xa0", " ", $typo);
...
@@ -230,10 +226,18 @@ abstract class TyposClientInterface
...
@@ -230,10 +226,18 @@ abstract class TyposClientInterface
$context = str_replace("\xc2\xa0", " ", $context);
$context = str_replace("\xc2\xa0", " ", $context);
$text = str_replace("\xc2\xa0", " ", $text);
$text = str_replace("\xc2\xa0", " ", $text);
*/
*/
// Clear texts from tags, replaces special characters with standard ones
// $typo = $this->clearText($typo);
// $corrected = $this->clearText($corrected);
$context
=
$this
->
clearText
(
$context
);
$text
=
$this
->
clearText
(
$text
);
// Find all typos in text, capture an offset of each typo
// Find all typos in text, capture an offset of each typo
if
(
!
preg_match_all
(
"#
{
$typo
}
#"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
))
{
$pattern
=
preg_quote
(
$typo
,
'#'
);
if
(
!
preg_match_all
(
"#
{
$pattern
}
#u"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
))
{
// Check for already fixed typo
// Check for already fixed typo
if
(
preg_match
(
"#
{
$corrected
}
#"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
))
{
$pattern
=
preg_quote
(
$corrected
,
'#'
);
if
(
preg_match
(
"#
{
$pattern
}
#u"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
))
{
throw
new
\Exception
(
"Already fixed"
,
208
);
throw
new
\Exception
(
"Already fixed"
,
208
);
}
}
...
@@ -243,7 +247,8 @@ abstract class TyposClientInterface
...
@@ -243,7 +247,8 @@ abstract class TyposClientInterface
// Find a context in text, capture it offset
// Find a context in text, capture it offset
if
(
!
preg_match
(
"#
{
$context
}
#"
,
$text
,
$contextMatch
,
PREG_OFFSET_CAPTURE
))
{
$pattern
=
preg_quote
(
$context
,
'#'
);
if
(
!
preg_match
(
"#
{
$pattern
}
#u"
,
$text
,
$contextMatch
,
PREG_OFFSET_CAPTURE
))
{
// If a context was changed then report an error,
// If a context was changed then report an error,
// cannot locate typo in a new context, must be
// cannot locate typo in a new context, must be
// fixed manually
// fixed manually
...
@@ -264,7 +269,8 @@ abstract class TyposClientInterface
...
@@ -264,7 +269,8 @@ abstract class TyposClientInterface
// Fix a match with index = $indexOfTypo
// Fix a match with index = $indexOfTypo
$index
=
0
;
$index
=
0
;
return
preg_replace_callback
(
"#
{
$typo
}
#"
,
$pattern
=
preg_quote
(
$typo
,
'#'
);
return
preg_replace_callback
(
"#
{
$pattern
}
#u"
,
function
(
$match
)
use
(
&
$index
,
$indexOfTypo
,
$corrected
)
{
function
(
$match
)
use
(
&
$index
,
$indexOfTypo
,
$corrected
)
{
$index
++
;
$index
++
;
if
((
$index
-
1
)
==
$indexOfTypo
)
{
if
((
$index
-
1
)
==
$indexOfTypo
)
{
...
@@ -276,4 +282,14 @@ abstract class TyposClientInterface
...
@@ -276,4 +282,14 @@ abstract class TyposClientInterface
$originalText
);
$originalText
);
}
}
/**
* Clears text from tags, replaces special characters with standard ones.
* Removes whitespace characters from the beginning and end of the text.
* In a sense, the function is the inverse for wptexturize(); - WordPress function.
*
* @param string $text
*
* @return string Cleared text
*/
protected
abstract
function
clearText
(
string
$text
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment