How to Separate a string (Arabic string)

Shafizadeh

I have a combined string that I want to separate.

My Pattern: (Arabic language, Starts from the right ):

str3[str2](str1)

Example 1

For the input:

string = تَ) [ ع . ] (مص م .) راست کردن ، معتدل کردن)

I want the output:

$str1='(تَ)';
$str2='[ ع . ]';
$str3='مص م .) راست کردن ، معتدل کردن)';

Example 2

For the input:

string = اِ تَ) (مص ل .) = اباته : شب را در جایی گذراندن)

I want the output:

$str1='(اِ تَ)';
$str2='';
$str3='مص ل .) = اباته : شب را در جایی گذراندن)';

Example 3

For the input:

string = [ ع . ] (مص م .) راست کردن ، معتدل کردن

I want the output:

$str1='';
$str2='[ ع . ]';
$str3='(مص م .) راست کردن ، معتدل کردن';

How can I do that?

someOne

As I have mentioned in the comments, the apparently first character (the rightmost) is not the open parenthesis as it supposed to be (in fact it's the last character), and this hidden error causes misunderstandings (it's just visually correct). However, the following code corrects the error and outputs the desired strings.

<?php
$arrStr = [
'تَ) [ ع . ] (مص م .) راست کردن ، معتدل کردن)',
'اِ تَ) (مص ل .) = اباته : شب را در جایی گذراندن)',
];
echo "<body style='direction: rtl !important;'>";
foreach($arrStr as $str) {
    preg_match('~(.*?\))(?:\s)(\[.*?\])?(?:\s*?)(.*)~', $str, $matches);
    $matches[1] = "(".$matches[1];
    $matches[3] = trim(substr($matches[3], 0, -1));
    echo "<pre>";
    for($i=1; $i<=3; $i++)
        echo "$i: {$matches[$i]}<br />";
    echo "</pre><hr>";
}
echo "</body>";
?>

The output: (Please note that the entries are in the correct RTL direction and will be displayed correctly on a RTL environment (they don't act falsify as being correct on a LTR environment.))

1: (تَ)
2: [ ع . ]
3: (مص م .) راست کردن ، معتدل کردن
_____________________________________________
1: (اِ تَ)
2: 
3: (مص ل .) = اباته : شب را در جایی گذراندن
_____________________________________________


P.S: So, here is your new scenario: The first part enclosed in () is optional, the second part enclosed in [] is also optional, but the third part is mandatory; According to your examples above, the third part may also start with a (*), Due to this, and considering the example of B (A) there is NO way to determine whether the example is in a format which has the optional first part (A) followed by the mandatory 3rd part B, or is in a format which doesn't have any of the optional parts but has the mandatory 3rd part being the whole string, if that's not a concern you may use the ~(.*?\)\s)?(\[.*?\]\s)?(.*)~ as the regular expression.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to check if string contains arabic character

From Dev

how to decode a string containing Persian/Arabic characters?

From Dev

How to separate a string with a char?

From Dev

How to separate string output?

From Dev

How to separate String

From Dev

Identify arabic characters in string

From Dev

newline for Arabic or urdu string

From Dev

Cut an arabic string

From Dev

newline for Arabic or urdu string

From Dev

Print arabic string in javascript

From Dev

How to use Parsec to separate a string by a specific string

From Dev

how to separate string by comma and store inside a string

From Dev

How separate string, c#

From Dev

How to split string into separate cells

From Dev

How to separate String array data?

From Dev

How to access separate characters in a string?

From Dev

How to separate a string and re build it

From Dev

How to separate a string with duplicated characters

From Dev

How to separate special string in android

From Dev

How to separate String array data?

From Dev

How to separate a string of repeating characters?

From Dev

PIL draw.text() is displaying string containing arabic ligature as two separate glyphs

From Dev

How to load arabic string.xml when device language is English

From Dev

How to remove english text from arabic string in python?

From Dev

How to map a arabic character to english string using python

From Dev

How to retrieve only arabic texts from a string using regular expression?

From Dev

How to convert Persian and Arabic digits of a string to English using JavaScript?

From Java

Converting a string with Arabic characters to a date

From Dev

Insert integer to Arabic String in Start

Related Related

HotTag

Archive