 |
อยากถามเรื่องการตัดสตริงหัว-ท้าย ตามอักขระที่กำหนดครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?php
echo substr("abcdef", -1)."<br>"; // returns "f"
echo substr("abcdef", -2)."<br>"; // returns "ef"
echo substr("abcdef", -3, 1)."<br>"; // returns "d"
?>
Go to : PHP substr()
|
 |
 |
 |
 |
Date :
2011-12-08 17:44:19 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมจะกำหนดให้เริ่มต้นด้วย // แล้วจบที่ /r/n ยังไงครับ
เพราะพารามิเตอร์ที่ 2 และ 3 มี data type เป็น int
|
 |
 |
 |
 |
Date :
2011-12-09 11:17:45 |
By :
runaway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค้นหาตำแหน่งของ string จากนั้นก็ใช้ substr ครับ มัน php มี function อยู่ครับ
|
 |
 |
 |
 |
Date :
2011-12-09 17:05:43 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เจอล่ะ strpos() ครับ
|
 |
 |
 |
 |
Date :
2011-12-09 17:06:07 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?
$str="The bird flys in through the windows.
//The cat jumps up onto the bed.
The dog is black.";
/*ผมอยากจะให้เหลือแค่
"The bird flys in through the windows.
The dog is black."
*/
$pos= strpos($str,'//');
$sub=substr($str,$pos,-17);
$result=str_replace($sub,'',$str);
echo $result;
?>
|
 |
 |
 |
 |
Date :
2011-12-10 10:57:10 |
By :
puchong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
text file?
|
 |
 |
 |
 |
Date :
2011-12-10 11:50:32 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
$s = "The bird flys in through the windows.\r\n//The cat jumps up onto the bed.\r\n//The rat jumps up onto the table.\r\nThe dog is black.//The rat jumps up onto the table.\r\n";
echo '<br>bfore:<pre>',$s,'</pre>';
$s = preg_replace('/^(\/\/[^\r\n]*\r\n)/m','',$s);
echo '<br>after:<pre>',$s,'</pre>';
?>
|
ประวัติการแก้ไข 2011-12-10 12:54:05
 |
 |
 |
 |
Date :
2011-12-10 12:52:51 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบคุณ ikikkok : ใช้ครับ text file
ขอบคุณทุกความเห็นมากครับ จะนำไปทดสอบดูครับ
|
 |
 |
 |
 |
Date :
2011-12-10 13:30:10 |
By :
runaway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|