1

In a book, I have words with hyphenation and I must delete those hyphens and connect the words. But we have a word with symbol "-". How can I delete hyphens in this situation?

Example text from a book:

$text = "
БАДРЎ(Й) (ي(بدرو он ки рўи нозебу хунук до-
рад, бадафт, безеб, бадбашара; муќоб.
хушрўй. БАДСОБИЌА بدسابقه он ки пешинаи хуб надо-
рад ва мардум ўро ба бадї мешиносанд. БАДСУРАТ بدصورت бадшакл, бадафт, бад-
андом; нозеб, зиштрўй. БАЙ-БАЙ بي بي нидо вањ-вањ, њай-њай (бо ало-
мати тањсин ва тааљљуб). БЕСАРУНЎГЇ بيسرونوگي 1. бесарунўг будан,
бетартибї, бесарусомонї. 2. чигилї, печи-
дагї, дарњам-барњамї. а) бесаранљом, парешонњол; б) бетартиб,
дарњаму барњам: бесару сомон шудани кор;
бесару тан белибос; луч, урён, барањна; бе-
сар-бесар гаштан худсарона овора гаштан.
";

I tried:

$search = array("-", "- ", " -", " - ");
$replace = array("", "", "", "");
$result = str_replace($search, $replace, $text);

Output:

БАДРЎ(Й) (ي(بدرو он ки рўи нозебу хунук до рад, бадафт, безеб, бадбашара; муќоб. хушрўй. 
БАДСОБИЌА بدسابقه он ки пешинаи хуб надо рад ва мардум ўро ба бадї мешиносанд.
БАДСУРАТ بدصورت бадшакл, бадафт, бад андом; нозеб, зиштрўй. 
БАЙБАЙ بي بي нидо вањвањ, њайњай (бо ало мати тањсин ва тааљљуб). 
БЕСАРУНЎГЇ بيسرونوگي 1. бесарунўг будан, бетартибї, бесарусомонї. 2. чигилї, печи дагї, дарњамбарњамї. а) бесаранљом, парешонњол; б) бетартиб, дарњаму барњам: бесару сомон шудани кор; бесару тан белибос; луч, урён, барањна; бе сарбесар гаштан худсарона овора гаштан.

The desired result would be like this:

БАДРЎ(Й) (ي(بدرو он ки рўи нозебу хунук дорад, 
бадафт, безеб, бадбашара; муќоб.
хушрўй. БАДСОБИЌА بدسابقه он ки пешинаи хуб надорад 
ва мардум ўро ба бадї мешиносанд. БАДСУРАТ بدصورت бадшакл, бадафт, 
бадандом; нозеб, зиштрўй. БАЙ-БАЙ بي بي нидо вањ-вањ, њай-њай (бо 
аломати тањсин ва тааљљуб). БЕСАРУНЎГЇ بيسرونوگي 1. бесарунўг будан,
бетартибї, бесарусомонї. 2. чигилї, печидагї, дарњам-барњамї. 
а) бесаранљом, парешонњол; б) бетартиб,
дарњаму барњам: бесару сомон шудани кор;
бесару тан белибос; луч, урён, барањна; 
бесар-бесар гаштан худсарона овора гаштан.
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
John
  • 468
  • 3
  • 16
  • your $search matches all occurrences of "-", thus it is impossible to have an output without remaining "-" – user1506104 Oct 08 '17 at 12:34
  • 1
    But how then will he put out such a condition: If next to "-" on both sides there are letters then do not touch them. And if there are no letters next to the "-" symbol on both sides (or at least on the left or on the right there is a space), then unite these pieces as the word's words. – John Oct 08 '17 at 12:39
  • 1
    If it does not happen then to divide all words with "-" if in the right and left of this symbol there is a letter. And in another case, if there is a space on the right of the symbol "-" then combine words like cels. – John Oct 08 '17 at 12:42

3 Answers3

3

I'm making an assumption: if the word is hyphenated in a way you want to undo, it will be at the end of the line.

You can use preg_replace() to do that:

$search = "/-\n/";
$replace = "";
preg_replace($search, $replace, $text);

This will search for any places where a "-" is followed by a newline character (if this came from a Windows-based system, you may need to replace "\r\n" instead of just "\n"), and replaces it with an empty string, thus removing the hyphen and the newline character.

Note that $search needs to be in double quotes for the "\n" to be parsed properly.

rickdenhaan
  • 10,857
  • 28
  • 37
  • `/-[\n\r]/` or even `-\R` might be what you were trying to articulate? In any case, +1. – Jan Oct 08 '17 at 13:27
  • Not `/-[\n\r]/` but perhaps `/-[\n\r]+/`. Althought `/-\R/` would be better, since it also catches `\f`, `\x0b` and `\x85` which are also sometimes used as linebreaks in legacy systems. – rickdenhaan Oct 08 '17 at 13:30
3

You may be looking for

-\R

This needs to be replaced by '', see a demo on regex101.com.


In PHP:
$regex = '~-\R~';
$new_text = preg_replace($regex, '', $text);

See a demo on ideone.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • Working this "/-\r\n/" and "/-\R/" and this '/(\w+)-(\s+)(\w+)/u' answers. Which one is faster? – John Oct 08 '17 at 13:21
  • @John: I'm pretty sure mine. The other needs more than 1000 steps to proceed (see a demo here: https://regex101.com/r/OxVeaO/2) while mine only needs 29. – Jan Oct 08 '17 at 13:25
  • 1
    For a text the size of your question, the speed difference is irrelevant. You might see fractions of a second in difference for a text 100x that size. – rickdenhaan Oct 08 '17 at 13:25
  • @rickdenhaan: Right you are but we do not know the text size. After all, it might as well be the holy bible... – Jan Oct 08 '17 at 13:26
  • Fair enough :-) – rickdenhaan Oct 08 '17 at 13:27
2

This is close to what you are looking for:

<?php

$string = "БАДРЎ(Й) (ي(بدرو он ки рўи нозебу хунук до-
рад, бадафт, безеб, бадбашара; муќоб.
хушрўй. БАДСОБИЌА بدسابقه он ки пешинаи хуб надо-
рад ва мардум ўро ба бадї мешиносанд. БАДСУРАТ بدصورت бадшакл, бадафт, бад-
андом; нозеб, зиштрўй. БАЙ-БАЙ بي بي нидо вањ-вањ, њай-њай (бо ало-
мати тањсин ва тааљљуб). БЕСАРУНЎГЇ بيسرونوگي 1. бесарунўг будан,
бетартибї, бесарусомонї. 2. чигилї, печи-
дагї, дарњам-барњамї. а) бесаранљом, парешонњол; б) бетартиб,
дарњаму барњам: бесару сомон шудани кор;
бесару тан белибос; луч, урён, барањна; бе-
сар-бесар гаштан худсарона овора гаштан.";

$pattern = '/(\w+)-(\s+)(\w+)/u';
$replacement = '${2}${1}${3}';
echo preg_replace($pattern, $replacement, $string);

?>

Sample output:

БАДРЎ(Й) (ي(بدرو он ки рўи нозебу хунук 
дорад, бадафт, безеб, бадбашара; муќоб.
хушрўй. БАДСОБИЌА بدسابقه он ки пешинаи хуб 
надорад ва мардум ўро ба бадї мешиносанд. БАДСУРАТ بدصورت бадшакл, бадафт, 
бадандом; нозеб, зиштрўй. БАЙ-БАЙ بي بي нидо вањ-вањ, њай-њай (бо 
аломати тањсин ва тааљљуб). БЕСАРУНЎГЇ بيسرونوگي 1. бесарунўг будан,
бетартибї, бесарусомонї. 2. чигилї, 
печидагї, дарњам-барњамї. а) бесаранљом, парешонњол; б) бетартиб,
дарњаму барњам: бесару сомон шудани кор;
бесару тан белибос; луч, урён, барањна; 
бесар-бесар гаштан худсарона овора гаштан.
user1506104
  • 6,554
  • 4
  • 71
  • 89