PHP หาค่าตัวอักษรใน string ทำไงครับ

เริ่มโดย Boya, 29 มีนาคม 2011, 14:28:13

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Boya

ผมต้องการเช็คว่าในข้อความที่รับมามี ตัวอักษรพิเศษที่ผมสร้างไว้เป็น array หรือไม่เขียนยังงัยครับ

$search = array("\\","\0","\n","\r","\x1a","'",'"');
$str เป็นข้อความที่รับมา

เช่น ถ้า

$str = "thaiseoboard's good"

ให้มัน return ว่า จริง

$str = "thaiseoboard is good"

ให้มัน return ว่า เท็จ

ขอบคุณครับ


Synchronize

แบบที่นึกออกได้ ทันทีเลยนะครับ
ก็ใช้ strpos()

เช่น


function _search($str)
{
     $search = array("\\","\0","\n","\r","\x1a","'",'"');
     foreach($search as $_v)
         if(strpos($str, $_v) !== false)
             return true;
    return false;
}



เวลาใช้ ก็

if( _search("thaiseoboard's good") ) 

ดู

  :wanwan017:
เนื้อหาความรู้น่าสนใจ ใหม่ๆ
[direct=https://develop.un-no.com/w3/docs/clear-float-in-div]เทคนิคการแก้ไขปัญหาจากการ float ซ้อน float แล้ว clear[/direct]

[direct=https://service.un-no.com/unbbz]ทำเว็บได้ด้วยตัวเองฟรี ด้วยระบบเว็บสำเร็จรูป unbbz , เว็บธรรมดา , เว็บบอร์ด , เว็บขายของ ทำได้หมดเลย[/direct]

Boya

อ้างถึงจาก: Synchronize ใน 29 มีนาคม 2011, 14:33:31
แบบที่นึกออกได้ ทันทีเลยนะครับ
ก็ใช้ strpos()

เช่น


function _search($str)
{
     $search = array("\\","\0","\n","\r","\x1a","'",'"');
     foreach($search as $_v)
         if(strpos($str, $_v) !== false)
             return true;
    return false;
}



เวลาใช้ ก็

if( _search("thaiseoboard's good") ) 

ดู

  :wanwan017:

ได้ละครับ ขอบคุณครับ

mean

loop กับ preg_match ไหนเร็วกว่ากันหน่อ ต้องลอง benchmark ดูครับ

$data ="\n";
if(preg_match("/(\\|\\0|\\n|\\r|\\x1a)/",$data)){
echo 'str found..';
}

ผมไม่ได้ลองครับ รบกวนลองที


[direct=https://www.ireallyhost.com/]บริการโดเมน, เว็บโฮสติ้ง, SSL Certificate[/direct]
[direct=https://www.ireallyhost.com/ssl]บริการ SSL Certificate , HTTPS สำหรับเว็บไซต์[/direct]
[direct=https://www.ireallyhost.com/]บริการ Web Hosting Free! Domain name ตลอดอายุบริการ [/direct]
[direct=https://www.ireallyhost.com/domain]บริการ จดโดเมน, Free DNS Service, Domain Forwarding, จัดการโดเมนได้เอง Domain Control Panel[/direct

xvlnw.com

ขอขุดเพื่อเป็นความรู้นะครับ

วิธีที่ง่ายที่สุดคือใช้ Function in_array(); ครับ

+http://php.net/manual/en/function.in-array.php
[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw