|
![](/images/resource/spacer.gif) |
|
เขียน PHP ให้เช็ค Expression ครับ ในกรณีที่เกิด Error ได้แก่ +2+8,5++5,8+9- ในกรณีที่ถูก 5+5,6+3+5*8 |
|
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
Code (PHP)
function check_formula( $s ){
echo preg_match( '/^[0-9]+([\+\-\*\/][0-9]+)+(,[0-9]+([\+\-\*\/][0-9]+)+)*$/' , $s) ? 'ok' : 'wrong';
echo '<pre>', print_r(explode(',', $s), true),'</pre>';
}
|
ประวัติการแก้ไข 2015-09-14 19:32:42 2015-09-14 19:40:14
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2015-09-14 19:05:56 |
By :
NewbiePHP |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
คำสั่งแยกดัวย element ใช้ explode() ลองหาอ่านดูครับ
เมื่อแยก ด้วย คอมม่าแล้ว
ก็ให้แยก ด้วย operator ก็จะได้ operand
แยกด้วย operand ก็จะได้ operator ลองดูครับ
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2015-09-14 20:50:30 |
By :
NewbiePHP |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
เข้าใจละครับ แต่ใน explode จะกำหนดยังไงให้ได้หลายๆตัวครับ ตัวต้องการแยก
Code (PHP)
<?php
function check_ex( $s )
{
$op = array('+','-','*','/');
$num = array('0','1','2','3','4','5','6','7','8','9');
echo preg_match( '/^[0-9]+([\+\-\*\/][0-9]+)+(,[0-9]+([\+\-\*\/][0-9]+)+)*$/' , $s) ? 'ok' : 'wrong';
echo "<br>";
echo "<br>";
if (!preg_match( '/^[0-9]+([\+\-\*\/][0-9]+)+(,[0-9]+([\+\-\*\/][0-9]+)+)*$/' , $s))
{
echo"Expression Invalid";
}
else
{
echo"Expression is valid";
echo "<br>";
echo"Operand";
echo '<pre>', print_r(explode('+', $s), true),'</pre>';
echo"Operator";
echo '<pre>', print_r(explode('0', $s), true),'</pre>';
}
}
check_ex("05*65+9+8");
?>
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2015-09-15 09:56:58 |
By :
meatspin |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
|
|
|
![](/images/digitalocean-banner.jpg)
|
Load balance : Server 02
|