|
 |
|
เรื่อง preg_replace ครับ ผมต้องการจะ แทนที่แท็ก <title></title> ใหม่ครับ แต่เขียน Regex ไม่ถูกเสียที งง ครับ ต้องเขียนอย่างไร |
|
 |
|
|
 |
 |
|
ใช้ pattern modifier s เพื่อให้ . match new line character ครับ
<?php
$title = " ทดสอบ.";
$string = "
<!-- title -->
<title>หัวข้อ</title>
<!-- end title -->
";
$pt = "/(<!-- title -->)(.*)(<!-- end title -->)/is";
$s = preg_replace($pt,"$1<title>{$title}</title>$3",$string);
echo htmlspecialchars($s);
?>
|
 |
 |
 |
 |
Date :
2009-06-30 23:19:38 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
$title = " ทดสอบ.";
$string = "
<!-- title -->
<title>หัวข้อ</title>
<!-- end title -->
";
$pt = "/(<!-- title -->)(.*)(<!-- end title -->)/is";
$s = preg_replace($pt,"$1<title>{$title}</title>$3",$string);
//echo htmlspecialchars($s);
echo $s;
?>
ทำไม <!-- end title --> มันหายไปครับ
คือต้องการคงค่าไว้ครับ
|
 |
 |
 |
 |
Date :
2009-07-01 05:49:18 |
By :
sutnet |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
$title = " ทดสอบ.";
$string = "
<!-- title -->
<title>หัวข้อ</title>
<!-- end title -->
";
$pt = "/(<!-- title -->)(.*)(<!-- end title -->)/is";
$s = preg_replace($pt,"$1<title>{$title}</title>$3",$string);
//echo htmlspecialchars($s);
echo $s;
?>
จากโค้ดข้างบนจะไม่เห็นข้อความ <!-- --> เพราะ <!-- --> เป็น comment ของภาษา html ครับ
แต่ถ้า view sourcecode น่าจะเห็นครับ
|
 |
 |
 |
 |
Date :
2009-07-01 08:27:19 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่า ได้แล้วครับ ขอบคุณมากครับ
ปล. ผมจะเอาไปทำการ Replace ค่า ลงใน Theme ของเว็บครับ
|
 |
 |
 |
 |
Date :
2009-07-01 08:54:29 |
By :
sutnet |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|