Boş alan PHP preg_replace html yorumlar

5 Cevap php

Bu gibi php kod biraz var:

$test = "<!--my comment goes here--> Hello World";

Now i want to strip the whole html comment from the string, i know i need to use preg_replace, but now sure on the regex to go in there. Can anybody help? Thanks

5 Cevap

$str=<<<'EOF'
<!--my comment goes here--> Hello World"
blah  <!-- my another
comment here --> blah2
end
EOF;

$r="";
$s=explode("-->",$str);
foreach($s as $v){
  $m=strpos($v,'<!--');
  if($m!==FALSE){
   $r.=substr($v,1,$m);
  }
}
$r.=end($s);
print $r."\n";

çıktı

$ php test.php
Hello World"
blah  < blah2
end

Yoksa preg_replace gerekir,

preg_replace("/<!--.*?-->/ms","",$str);

Denemek

 preg_replace('~<!--.+?-->~s', '', $html);
preg_replace('/<!--(.*)-->/Uis', '', $html)

$html dizesinde bulunan her html açıklamayı kaldırmak olacaktır. Bu yardımcı olur umarım!

Eğer gibi arasında içerik ile 2 yorumlarınız vermezsek bu sadece çalışır ...

<!--comment--> Im a goner <!--comment-->

İhtiyacınız ...

//preg_replace('/<!--[^>]*-->/', '', $html); // <- this is incorrect see ridgrunners comments below, you really need ...
preg_replace('/<!--.*?-->/', '', $html);

The [^>] matches anything but > so as to not go past the matching > seeking the next. I havent tested phps regex but it claims to be perl regex which is by default 'greedy' and will match as much as possible.

Ama sen özel olarak adlandırılmış yer tutucu eşleşen beri sadece yerine str_replace () kullanmak için tüm dizeyi ve gerekir.

str_replace('<!--my comment goes here-->', $comment, $html);

Ve, yerine bir dosyada yer tutucular yerine daha sadece bir php dosyası yapmak ve değişkenleri yazmak.

:)

<?php
$test = "<!--my comment goes here--> Hello World";
echo  preg_replace('/\<.*\> / ','',$test);
?>

Global yerine için aşağıdaki kodu kullanabilirsiniz:

<?php
$test = "<!--my comment goes here--> Hello World <!--------welcome-->welcome";
echo  preg_replace('/\<.*?\>/','',$test);
?>