3

I explain my question by one example:

Example:

(English version)

$str = '1 healthy, fine, sound 2 tip-top, fit, robust, sturdy & 1  substandard, poor';

I want this output:

- healthy, fine, sound
- tip-top, fit, robust, sturdy
&
- substandard, poor

(Persian version) - It starts from the right

$str ='1 خشكاندن، خشكانيدن 2رطوبتزدايي كردن، نمزدايي كردن & تر کردن';

I want this output:

- خشكاندن، خشكانيدن
- رطوبتزدايي كردن، نمزدايي كردن
&
تر کردن

I know, 1 in this example is really vague. But actually it is the first character in the right. But in SO, because there isn't a proper direction for Persian, 1 goes in the left.


Note: Perhaps there are no numbers or even no &. In this case I don't want any change ..!

$str = 'healthy, fine, sound, tip-top'; // I want this: healthy, fine, sound, tip-top


$str = 'healthy, fine, sound & poor'; /* I want this: healthy, fine, sound
                                                      &
                                                       poor
                                      */

I can do that using multiple str_replace(). But I want to know, is it possible to I do that using REGEX?

2 Answers 2

3

This script works for both the English & the Persian variant.

$str = "1 خشكاندن، خشكانيدن 2رطوبتزدايي كردن، نمزدايي كردن & تر کردن";
// $str = '1 healthy, fine, sound 2 tip-top, fit, robust, sturdy & 1  substandard, poor';
$dashed_line = preg_replace("/[0-9]+/", "- ", $str);
$lines = preg_replace("/(?<!^)- /", "\n- ", $dashed_line);
$lines_with_and = preg_replace("/&/", "\n&\n", $lines);
$lines_with_and_single_space = preg_replace("/ +/", " ", $lines_with_and);
echo preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $lines_with_and_single_space);
Sign up to request clarification or add additional context in comments.

2 Comments

Your solution is fine. +1. Just there is a small problem. line 9
Just remove the actual newline (enter) from your string. In other words, define your variable $str in a single line.
1

I haven't tested the Persian variant as I have no experience with RTL scripts, but it should work for the English example.

// first let's put all the numbers to a new line
$digit_split_str = preg_replace('/(?<!^)(?=\d)/', "\n", $str);
// then ampersands
$ampersand_split_str = preg_replace('/(?<!^)&/', "\n&\n", $digit_split_str);
// then let's replace the numbers at the start of the string with dashes
$dashed_str = preg_replace('/^\d+/m', "- ", $ampersand_split_str);

EDIT: fixed corner cases around ampersands and newlines...

2 Comments

Well, According to what I tested and as you mentioned, it doesn't work for Persian context. I haven't tested it for English so far. But for Persian, it cuts the string from somewhere ...!
I can't help you with that, my editor can't handle RTL input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.