ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: joke038 ที่ 01 ธันวาคม 2013, 08:36:28



หัวข้อ: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: joke038 ที่ 01 ธันวาคม 2013, 08:36:28
คือผมอยากถามถ้าเราต้องการจะตัดคำสี่ไส่ไปใน input ต้องทำไงครับสมุติ
bababa.test.com

พอใส่ใน  input  ละพอกด ปุ่มมันก็จะตัดเอาแค่ test.com ไปครับ

<input id="test" align="center" class="form" type="text" name="test" required placeholder="test">


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: phaishow ที่ 01 ธันวาคม 2013, 08:59:30
จากตัวอย่าง ถ้าเป็นผม ผมจะใช้วิธีนี้ครับ
โค๊ด:
//input = bababa.test.com
$test = $_REQUEST['test']; // หรือ $_POST['test'] หรือ $_GET['test'] ตาม method ที่กำหนดไว้จาก form
$exp = explode('.',$test); // แบ่งคำด้วย .
array_shift($exp); // เอาค่าแรกออก
$test = implode('.',$exp); // ต่อคำที่เหลือด้วย . จะได้ $test = test.com

แถมให้อีกวิธีครับ อันนี้ง่ายหน่อย

โค๊ด:
$input = 'bababa.test.com';
preg_match('/(|^.)test.com/', $input, $output);
$test = $output[0]; // จะได้ $test = test.com


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: joke038 ที่ 01 ธันวาคม 2013, 09:32:32
ขอบคุณครับ


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: joke038 ที่ 01 ธันวาคม 2013, 09:44:12
มีแบบนับตัวอักษรแล้วตัดไมครับ แบบว่า
test.test.com
ตัดคำ 4 ตัวแรกเหลือนแค่ test.com :wanwan017:


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: phaishow ที่ 01 ธันวาคม 2013, 09:47:10
ถ้าจะแค่ 4 ตัวอักษร ทุก input ใช้ substr ครับ

โค๊ด:
$input = 'test.test.com';
$test = substr($input,5); // นับรวม . ด้วย จะได้ $test = test.com


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: joke038 ที่ 01 ธันวาคม 2013, 12:38:14
งงครับ
ขอเป็นแบบ code php แบบสำเร็จเลยได้ไมครับ


หัวข้อ: Re: สอบถามเกี่ยวกับ input
เริ่มหัวข้อโดย: phaishow ที่ 01 ธันวาคม 2013, 12:53:57
ก็โค้ดด้านบนแหละครับ ลองดูหรือยังครับ

ถ้า $input = 'test.test.com';
โค๊ด:
$test = substr($input,5);
ตัวแปร $test จะได้ test.com

ถ้า $input = 'abcdefg.test.com';
โค๊ด:
$test = substr($input,5);
ตัวแปร $test จะได้ fg.test.com

ถ้า $input = '123456789.test.com';
โค๊ด:
$test = substr($input,5);
ตัวแปร $test จะได้ 6789.test.com