PHP dize ayrıştırma

3 Cevap

Nasıl bu satırı bölebilirsiniz:

我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him

Böyle içine üç satır:

我 [wǒ] - (pronoun) I or me

你 [nǐ] - (pronoun) you (second person singular); yourself

他 [tā] - (pronoun) he or him

Kullanıcının her satırdan sonra <br /> etiketini eklemeniz diyelim?

Teşekkür ederiz!

UPD. Benim kötü dönemler vardı, ama o bir hataydı.

3 Cevap

Eğer noktalar kaldırıldı beri gördüğümüz tek açık model "yabancı bir karakteri, bir boşluk ve bir açma aparatı" dir.

Bu konuda odak edelim:

<?php

$string = "我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him";

$result = preg_replace('/(. \[)/u', // "any char, a space then [", 'u' flag to use UTF8 
                       '<br/>$1', // replace it by a break table and a back reference
                        $string);

echo $result;

Note that using this algo, the line breaks will be place at the begining of the lines. Don't forget the UTF-8 flag, and use UTF-8 everywhere in your application or processing strings will be a mess.

EDIT: Hiç satır sonu sadece iki satır başında olmak istiyorsa, o zaman bu amaç için negative lookbehind kullanabilirsiniz:

$string = "我 [wǒ] - (pronoun) I or me 你 [nǐ] - (pronoun) you (second person singular); yourself 他 [tā] - (pronoun) he or him";

// the same pattern, but excluding the one preceded by "^", where the string starts
$result = preg_replace('/(?<!^)(. \[)/u',   
                       '<br/>$1', 
                        $string);

echo $result;

Eğer biçimi konusunda emin değilseniz, böyle bir şey deneyebilirsiniz, ancak uygun bir sınırlayıcı olmadan hepsi sadece tahmin ve neler yanlış dönüşüm alabilirsiniz.

$str = preg_replace("/\s+(\S+\s+\[\S+\])/", "<br />$1", $str);

Benim yorumum doğru ise, sadece her Japonca / Çince karakter önce kırmak istiyorsun?

Php kılavuzda, ord fonksiyonu yorumların bir UTF-8 Ord fonksiyonu için öneriler / kod vardır. Böyle bir fonksiyon ile dize ile UTF-8 codepoint UTF-8 kodlaması altında yineleme, ve kimin ord> Japonca / Çince karakterlerin başlayacak bir kodlaması altında (karakter), karşılaştığınızda ilk
falan ekleyebilirsiniz.

Edit: Ord için doc sayfası here

Ve bu bence kodu sorunu için uygun olabilir olduğu: dot shetline de yazar kerry teklif com

Here's my take on an earlier-posted UTF-8 version of ord, suitable for iterating through a string by Unicode value. The function can optionally take an index into a string, and optionally return the number of bytes consumed by a character so that you know how much to increment the index to get to the next character.

<?php

function ordUTF8($c, $index = 0, &$bytes = null)
{
  $len = strlen($c);
  $bytes = 0;

  if ($index >= $len)
    return false;

  $h = ord($c{$index});

  if ($h <= 0x7F) {
    $bytes = 1;
    return $h;
  }
  else if ($h < 0xC2)
    return false;
  else if ($h <= 0xDF && $index < $len - 1) {
    $bytes = 2;
    return ($h & 0x1F) <<  6 | (ord($c{$index + 1}) & 0x3F);
  }
  else if ($h <= 0xEF && $index < $len - 2) {
    $bytes = 3;
    return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
                             | (ord($c{$index + 2}) & 0x3F);
  }          
  else if ($h <= 0xF4 && $index < $len - 3) {
    $bytes = 4;
    return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
                             | (ord($c{$index + 2}) & 0x3F) << 6
                             | (ord($c{$index + 3}) & 0x3F);
  }
  else
    return false;
}

?>