0

How to eliminate\remove last character and space after that and to merge splitted word in php after using OCR for scanning documents

Tried with rtrim, replace etc.. But it also delete - on beginning of text

$delete = array('-');

if(in_array($string[(strlen($string)-1)], $delete))
    $string = substr($string, 0, strlen($string)-1);

This is an example of text after ocr scanning

'Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good'


You know how it should be

This is what is looking like after document ....

Like I said I tried with replace but "-" sign is also removed from begging of text...

Idea is to remove "- " (dash and space) between splitted word and to marge word again

user3836412
  • 11
  • 1
  • 5

2 Answers2

1

This can be accomplished with preg_replace.

$s = 'Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good';
$s = preg_replace('/- /','',$s);
echo preg_replace('/ -/',". -\n",$s);

This is what is looking like after document is scanned. 
-And it not looking good
Jason K
  • 1,406
  • 1
  • 12
  • 15
  • I did not pay attention to this part of text -And it not look- ing good it should be an new row and dash at beginning row should stay :) So after preg_replace should be like: This is what is looking like after document is scanned . -And it not looking good – user3836412 Sep 19 '19 at 14:59
0

A string is not an array in the strict sense, but there exists in-built php functions to convert one into another. They are explode() and implode()

The code below solves your problem.

<?php

    $string = "Th- is is wh- at is looking like after doc- ument is scanned -And it not look- ing good";
    //$delete = array('-');
    $string_array = explode('- ',$string);
    $string_new = implode($string_array);
    echo $string_new;
Krish PG
  • 105
  • 9
  • `echo implode(explode('- ',$string));` will also work for one line code magic and showing off to your colleagues – Krish PG Sep 19 '19 at 14:43