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
92e17703
Commit
92e17703
authored
May 28, 2018
by
Георгий Попов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add an implementation of replaceTypoInArticle method
parent
53e5cab9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
+35
-0
TyposClientInterface.php
src/TyposClientInterface.php
+35
-0
No files found.
src/TyposClientInterface.php
View file @
92e17703
...
...
@@ -62,6 +62,40 @@ abstract class TyposClientInterface
* @param TyposArticle $article Article to fix the typo
*/
private
function
replaceTypoInArticle
(
string
$typo
,
string
$corrected
,
string
$context
,
TyposArticle
$article
)
{
$article
->
text
=
"<p>Text article. <span>Contain</span> some tags and article<b>typo</b>. What to correct.</p>"
;
// Strip all tags from text
$text
=
strip_tags
(
$article
->
text
);
// Find all typos in text, capture an offset of each typo
$typos
=
[];
preg_match
(
"#
{
$typo
}
#"
,
$text
,
$typos
,
PREG_OFFSET_CAPTURE
);
// Find a context in text, capture it offset
$contextMatch
=
[];
preg_match_all
(
"#
{
$context
}
#"
,
$text
,
$contextMatch
,
PREG_OFFSET_CAPTURE
);
$contextOffset
=
$contextMatch
[
0
][
1
];
// Find a concrete typo that we want to fix
$indexOfTypo
=
null
;
foreach
(
$typos
as
$index
=>
$typo
)
{
$typoOffset
=
$typo
[
1
];
if
(
$typoOffset
>=
$contextOffset
)
{
$indexOfTypo
=
$index
;
break
;
}
}
// Fix a match with index = $indexOfTypo
$index
=
0
;
preg_replace_callback
(
"#
{
$typo
}
#"
,
function
(
$match
)
use
(
&
$index
,
$indexOfTypo
,
$corrected
)
{
if
(
$index
==
$indexOfTypo
)
{
return
$corrected
;
}
$index
++
;
return
$match
[
0
];
},
$article
->
text
);
}
}
\ No newline at end of file
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