2017-01-05 9 views
0

段落がある場合は、3等分(文字単位)に分割する必要があります。PHPで段落の文字を3等分して分割する

これは私が必要なもの、私の文字列

$string="this is my long text this is my long text this 
     is my long text this is my long text 
     this is my long text this is my 
     long text this is my long text this is my long text 
     this is my long text this is my long text"; 

では、次のとおりです。3つの部品やパート1で

  • 35%、パート2は35%とその3で30%(中

    • イコール文字3部 - 単語または文字列ではない文字)

  • +0

    あなたは自分のソリューションを実装しようとしたことがありますか? –

    +0

    **スペース**は考慮されるかどうか? –

    +0

    ** chunk_split **を使用してください。 – Dave

    答えて

    1

    チェックも

    $string="this is my long text this is my long text this 
         is my long text this is my long text 
         this is my long text this is my 
         long text this is my long text this is my long text 
         this is my long text this is my long text"; 
    
    $strlen=strlen($string); 
    
    $first= intval($strlen * (35/100)); 
    $second=intval($strlen * (35/100)); 
    $third=intval($strlen * 30/100); 
    
    
    
    $first_part=substr($string,0,$first); 
    $second_part=substr($string,$first,$second); 
    $third_part=substr($string,($first+$second)); 
    
    +1

    いいこと、あなたはintel broを使っています:) –

    +1

    まさに私が欲しいもの – condition0

    0

    あなたは次のようにコーディングすることにより求めたものを達成することができますより良い使用方法については

    $string="this is my long text this is my long text this 
        is my long text this is my long text 
        this is my long text this is my 
        long text this is my long text this is my long text 
        this is my long text this is my long text"; 
    
    
    $strlen = strlen($string); 
    
    
    
    $strlen1 = floor((35/100) * $strlen); 
    $strlen2 = $strlen1 + floor((35/100) * $strlen); 
    $strlen3 = $strlen2 + floor((30/100) * $strlen); 
    
    echo substr($string,0,$strlen1); 
    echo"<br>"; 
    echo substr($string,$strlen1,$strlen2); 
    echo "<br>"; 
    echo substr($string,$strlen2,$strlen3); 
    

    あなたが分割さの文字列を返す関数の中でそれをラップすることができます:)

    0
    <?php 
    $string = "this is my long text this is my long text this 
        is my long text this is my long text 
        this is my long text this is my 
        long text this is my long text this is my long text 
        this is my long text this is my long text"; 
    $arr1 = str_split($string); 
    
    $pieces = array_chunk($arr1, ceil(count($arr1)/3)); 
    echo explode($pieces[0],'');// this will convert the array back into string you wish to have. 
    echo explode($pieces[1],''); 
    echo explode($pieces[2],''); 
    
    
    ?> 
    

    私がテストしていませんそれ。しかし、このやり方で我々はそれを行うことができます。 最初に文字列を配列に分割し、array_chunkで半分に分割します。

    ここで$個の部分を印刷するとします。各インデックスにスライスされた入力が表示されます。

    1

    この1 mb_strlen()mb_substr()機能を使用してソリューション:

    $chunk_size = round(mb_strlen($string) * 0.35); 
    $parts = []; 
    foreach ([0, $chunk_size, $chunk_size * 2] as $pos) { 
        $parts[] = mb_substr($string, $pos, $chunk_size); 
    } 
    print_r($parts); 
    

    出力:

    Array 
    (
        [0] => this is my long text this is my long text this 
         is my long text this is my lon 
        [1] => g text 
         this is my long text this is my 
         long text this is my long tex 
        [2] => t this is my long text 
         this is my long text this is my long text 
    ) 
    
    関連する問題