-1

<p>各配列値の先頭にタグを追加し、各配列値</p>の末尾に終了タグを追加する必要があります。

[または区切り文字がある場合は、]それらを置き換える必要があります<p class="myclass">

Array
(
    [0] => [This is a line of text
    [1] => and another
    [2] => and yet another.] [This is another line of text
    [3] => and another
    [4] => and another] [OK, so you get the idea.
)

上記の配列は次のようになります。

Array
(
    [0] => <p class="myclass">This is a line of text</p>
    [1] => <p>and another</p>
    [2] => <p>and yet another.</p> <p class="myclass">This is another line of text</p>
    [3] => <p>and another</p>
    [4] => <p>and another</p> <p class="myclass">OK, so you get the idea.</p>
)

問題は、foreach ループを使用して、最初の配列から 2 番目の配列に移動するにはどうすればよいかということです。

4

2 に答える 2

3
$myArray = array(
    '[This is a line of text',
    'and another',
    'and yet another.] [This is another line of text',
    'and another',
    'and another] [OK, so you get the idea.',
);

array_walk($myArray,'pTagger');

function pTagger(&$value) {
    $value = str_replace(array('[',']'),array('<p class="myClass">','</p>'),$value);
    if (substr($value,0,2) !== '<p') $value = '<p>' . $value;
    if (substr($value,-4) !== '</p>') $value .= '</p>';
}

var_dump($myArray);
于 2012-12-04T21:18:10.523 に答える
1
for($i = 0; $i < count($array); $i++) {
  $array[$i] = '<p>'.$array[$i].'</p>';
  $array[$i] = preg_replace('/\]/', '</p>', $array[$i]);
  $array[$i] = preg_replace('/\[/', '<p class="myclass">', $array[$i]);
  $array[$i] = preg_replace('/<p><p/', '<p', $array[$i]);
}

実際の例を見る

于 2012-12-04T21:13:51.393 に答える