Br ile satırsonlarını değiştirin ama PHP h1 sonra satırsonu görmezden

2 Cevap php

O <br> etiketi ile satırsonu yerine, ama bir </h1> etiketini takip satırsonlarını görmezden mümkün mü?

Yani, örneğin ben bu bloğu değiştirmek istiyorum:

<h1>Test</h1> \n some test here \n and here

Buna:

<h1>Test</h1> some test here <br /> and here

2 Cevap

$subject = "<h1>hithere</h1>\nbut this other\nnewline should be replaced.";
$new = preg_replace("/(?<!h1\>)\n/","<br/>",$subject);
echo $new;

// results in:
// <h1>hithere</h1>
// but this other<br/>newline should be replaced.

Çalışmalıdır. Bu diyor ki \ n h1 hemen preceeded değil>

Yerine düzenli ifadeler string fonksiyonlarını kullanarak, ve </h1> etiketi (yerine sadece yeni satır daha önce) hattı üzerinde herhangi bir yerde olabilir varsayarak:

$lines=file($fn);
foreach ($lines as $line) {
  if (stristr("$line", "</h1>") == FALSE) {
    $line = str_replace("\n", "<br />", $line);
  }
  echo $line;
}

bu, örnek için, ile sonuçlanır:

<h1>Test</h1>
some test here <br />and here<br />