使用PHP对“\n”进行分解时,从“\n\n”返回的字符是什么?
我在数据库中添加了一些数据。文本包括英文和阿拉伯文。所以,虽然我需要在网页中显示文本,但我试图将整个文本拆分为段落 然后,如果段落包含阿拉伯语文本,则显示文本 在从右到左对齐的div中。 如果文本为英文,则以从左到右的对齐方式显示。 以下是我的代码:使用PHP对“\n”进行分解时,从“\n\n”返回的字符是什么?,php,html,css,Php,Html,Css,我在数据库中添加了一些数据。文本包括英文和阿拉伯文。所以,虽然我需要在网页中显示文本,但我试图将整个文本拆分为段落 然后,如果段落包含阿拉伯语文本,则显示文本 在从右到左对齐的div中。 如果文本为英文,则以从左到右的对齐方式显示。 以下是我的代码: /*------ Allign text ------*/ function align_text($text) { $string = $text; $returnString = ''; $paraArray = exp
/*------ Allign text ------*/
function align_text($text)
{
$string = $text;
$returnString = '';
$paraArray = explode("\n", $string);
if(count($paraArray) > 0)
{
foreach($paraArray as $pa)
{
$isRight = $this->is_rtl($pa);
if($isRight)
{
$para['para'] = $pa;
$para['align'] = 'R';
}
else
{
$para['para'] = $pa;
$para['align'] = 'L';
}
$paraArr[] = $para;
}
}
else
{
}
return $paraArr;
}
function is_rtl($string)
{
$rtl_chars_pattern = '/[\x{0590}-\x{05ff}\x{0600}-\x{06ff}]/u';
return preg_match($rtl_chars_pattern, $string);
}
现在,问题来了
Array (
[0] => Array (
[para] => This is testing mail.
[align] => L )
[1] => Array (
[para] => This is testing mail. This is testing mail. This is testing mail.
[align] => L )
[2] => Array (
[para] =>
[align] => L )
[3] => Array (
[para] => This is testing mail.
[align] => L ) );
如您所见,从第0行开始计数的第2行if具有{[para]=>}。
这是因为\n\n即两个连续的换行符或
我在这里扮演的角色是什么
当试图这样显示结果时
<?php
if(!is_array($row['chatDesc']))
{?>
<div class="com-text">
<?php echo $row['chatDesc'];?>
</div>
<?php
}
else
{
foreach($row['chatDesc'] as $rC)
{
echo strlen($rC['para']);
if(strlen($rC['para'])==1 &&
($rC['para']==null || $rC['para']=="" || $rC['para']==" " || $rC['para']=="\n"))
{?>
<div class="com-text" style="direction:rtl; clear:both">
[AAA<?php echo $rC['para'];?>AAA]
</div>
<?php
}
else if($rC['align']=='R' && $rC['para']!=' ')
{?>
<div class="com-text" style="direction:rtl; clear:both">
[<?php echo $rC['para'];?>]
</div>
<?php
}
else if($rC['align']=='L' && $rC['para']!=' ')
{?>
<div class="com-text" style="direction:ltr;clear:both">
[<?php echo $rC['para'];?>]
</div>
<?php }
}
} ?>
但如果我改变条件
if(strlen($rC['para'])==1 &&
($rC['para']==null || $rC['para']=="" || $rC['para']==" " || $rC['para']=="\n"))
仅当ifstrlen$rC['para']==1时,将显示div
div的内容为[AAA]。两对AAA之间有一个额外的空间。如果这个字符确实是a,那么为什么If语句中的初始条件不满足要求?我已经尝试了您提供的If语句,但是它对我很好。作为修复所有空白字符的解决方案,我对函数进行了一些重构,以从段落中删除任何不必要的空白,并仅在段落不为空时将其推送到数组中
function align_text($text){
$result = array();
foreach(explode("\n", $text) as $paragraph){
// Remove any whitespace from the beginning and end of the paragraph
$paragraph = trim($paragraph);
// Only append the paragraph if it's not empty
if(strlen($paragraph) > 0)
array_push($result, array('para' => $paragraph, 'align' => is_rtl($paragraph) ? 'R' : 'L'));
}
return $result;
}
而且,在HTML中显示div不需要那么多if case:
<?php foreach($row['chatDesc'] as $rC): ?>
<div class="com-text" style="direction:<?php echo $rC['align'] == 'L' ? 'ltr' : 'rtl' ?>;clear:both;">
<?php echo $rC['para']; ?>
</div>
<?php endforeach; ?>
然后在html中,您可以简单地回显align:
<?php foreach($row['chatDesc'] as $rC): ?>
<div class="com-text" style="direction:<?php echo $rC['align'] ?>;clear:both;">
尝试使用chr函数定义symbol Start,方法是使用var_dump而不是print_r来查看有关变量类型、长度和内容的更多详细信息。换句话说,var_dump$rC应该为您提供您所需要的。根据您的源数据,不可见字符可能是\r或其他类型的空白字符。可能是\r或空格。@kb。我得到了var_dump声明的{'para'=>string1.@Saswat我刚刚尝试了你的if语句,它对我来说很好。
array_push($result, array('para' => $paragraph, 'align' => is_rtl($paragraph) ? 'rtl' : 'ltr'));
<?php foreach($row['chatDesc'] as $rC): ?>
<div class="com-text" style="direction:<?php echo $rC['align'] ?>;clear:both;">