如何在PHP中将阿拉伯语与字母分开?

时间:2017-07-28 16:05:15

标签: php html

如果您尝试此链接http://phpfiddle.org/lite/code/ndra-up85(请逐一提交)或此完整代码

<style>
        .ENGLISH {
            direction: ltr;
        }
        .ARAB {
            direction: rtl;
        }
</style>

<?php
function bbc2html($content) {
    $search = array(
        '/(\r\n\r\n|\n\n|\r\r|\n\r\n\r){1,}/',
        '/(\r\n){1,}/',
        '/(.*?)\001/',
        '/(.*?)\002(.*?)/',
        '/(\[arab\])(.*?)(\[\/arab\])/',
        '/(\[b\])(.*?)(\[\/b\])/',
        '/(\[i\])(.*?)(\[\/i\])/',
        '/(\[u\])(.*?)(\[\/u\])/',
        '/(\[ul\])/',
        '/(\[\/ul\])/',
        '/(\[ol\])/',
        '/(\[\/ol\])/',
        '/(\[li\])(.*?)(\[\/li\])/',
        '/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
        '/(\[url\])(.*?)(\[\/url\])/'
    );
    $replace = array(
        "\001",
        "\002",
        '<p class="ENGLISH">$1</p>',
        ' $1',
        '<span class="lbs0">$2</span>',
        '<strong>$2</strong>',
        '<em>$2</em>',
        '<u>$2</u>',
        '<ul>',
        '</ul>',
        '<ol>',
        '</ol>',
        '<li>$2</li>',
        '<a href="$2" target="_blank">$4</a>',
        '<a href="$2" target="_blank">$2</a>'
    );

    $oriText = trim($content);
    $arrText = preg_split("/\\n\\n/", $oriText);
    $newText = "";

    foreach($arrText as $text) {
        $newText.= trim($text) . "\n\n";
    }

    $newText = preg_replace("%([\u{0600}-\u{06FF}]+(?:\s+[\u{0600}-\u{06FF}]+)*)%", ' <b>$1</b>', $newText);

    return preg_replace($search, $replace, $newText);
}

?>

    <form name="form1" method="post" action="">
        <p>
            <textarea name="mytxtarea" id="mytxtarea" class="ed">This is a sample textssss

فَإِذَا جَلَسْتَ فِي وَسَطِ الصَّلَاةِ فَاطْمَئِنَّ، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُمَّ تَشَهَّدْ

            </textarea>
        </p>
        <p id="posttextareadisplay"></p>
        <p>
            <input type="submit" name="Submit" value="Submit">
        </p>
    </form>
<?php echo html_entity_decode(bbc2html($_POST['mytxtarea'])); ?>

<br><br><br><br><br><br>

    <form name="form1" method="post" action="">
        <p>
            <textarea name="mytxtarea2" id="mytxtarea2" class="ed">This is a sample textssss
فَإِذَا جَلَسْتَ فِي وَسَطِ الصَّلَاةِ فَاطْمَئِنَّ، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُمَّ تَشَهَّدْ

            </textarea>
        </p>
        <p id="posttextareadisplay"></p>
        <p>
            <input type="submit" name="Submit" value="Submit">
        </p>
    </form>

<?php echo bbc2html($_POST['mytxtarea2']);?>

稍后结果将是这样的

PART 1
<p id="posttextareadisplay">
    <p class="ENGLISH">This is a samplasde textssss</p>
    <p class="ENGLISH"><b>فَإِذَا جَلَسْتَ فِي وَسَطِ الصلَاةِ فَاطْمَئِن، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُم تَشَهدْ</b></p>
</p>

PART 2
<p id="posttextareadisplay">
    <p class="ENGLISH">This is a samplasde textssss <b>فَإِذَا جَلَسْتَ فِي وَسَطِ الصلَاةِ فَاطْمَئِن، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُم تَشَهدْ</b></p>
</p>

问题第1部分:如果您看到以下代码,则所有paragraphs<p>均为class="ENGLISH"

如果段落中的内容是阿拉伯语,如何将其变为paragraph<p> class="ENGLISH"class="ARAB"?但如果写作不是阿拉伯语,那么class="ENGLISH"

段落
PART 1
<p id="posttextareadisplay">
    <p class="ENGLISH">This is a samplasde textssss</p>
    <p class="ENGLISH"><b>فَإِذَا جَلَسْتَ فِي وَسَطِ الصلَاةِ فَاطْمَئِن، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُم تَشَهدْ</b></p>
</p>

*****     I WANT TO BE LIKE THIS     *******
<p id="posttextareadisplay">
    <p class="ENGLISH">This is a samplasde textssss</p>
    <p class="ARAB"><b>فَإِذَا جَلَسْتَ فِي وَسَطِ الصلَاةِ فَاطْمَئِن، وَافْتَرِشْ فَخِذَكَ الْيُسْرَى ثُم تَشَهدْ</b></p>
</p>

问题第2部分:但是如果阿拉伯语加入普通文字或普通字体。因此段落或<p>仍为class="ENGLISH"

喜欢这个

<p class="ENGLISH"> This is a sa <b>  لْيُسْرَى ثُم تَشَهدْ</b></p>
<p class="ENGLISH"><b>  لْيُسْرَى ثُم تَشَهدْ</b> This is a sa</p>
<p class="ENGLISH"> This is a sa <b> ا لْيُسْرَى ثُم تَشَهدْ</b> This is a sa</p>
<p class="ENGLISH"><b>  لْيُسْرَى ثُم تَشَهدْ</b> This is a sa <b>  لْيُسْرَى ثُم تَشَهدْ</b></p>

0 个答案:

没有答案