Site icon บริษัท โค๊ดบี จำกัด

จัดการกับ string ใน php codeigniter ด้วย string helper

จัดการกับ-string-ใน-php-codeigniter-ด้วย-string-helper

จัดการกับ-string-ใน-php-codeigniter-ด้วย-string-helper

&NewLine;<figure class&equals;"wp-block-video"><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">วิธี จัดการกับ string ใน php codeigniter ด้วย string helper เราสามารถจัดการกับตัวแปรหรือข้อความตัวหนังสือ ข้อความแบบตัวเลข ที่อยู่ในรูปแบบของ string เช่นการแรนดอมข้อมูล การตั้งชื่อไฟล์ข้อมูลไม่ให้ซ้ำ เป็นต้น<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading">วิธีใช้งาน String helper<&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">วิธีใช้งานไม่มีอะไรซับซ้อนครับ เพียงใช้คำสั่งโหลดตัว helper เข้ามาใช้งานเท่านั้นเอง<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;this<strong>-><&sol;strong>load<strong>-><&sol;strong>helper&lpar;'string'&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;30px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading">คำสั่งใน String helper<&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>string&lowbar;random<&sol;strong> ใช้สำหรับแรนดอมสตริง ตัวอย่างการใช้งาน เช่น<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php"><strong>echo<&sol;strong> random&lowbar;string&lpar;'alnum'&comma; 16&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ จะได้ข้อมูลกลับมาจำนวน 16 ตัว โดยแรนดอมจากตัวอักษรภาษาอังกฤษ ทั้งตัวพิมพ์เล็ก ตัวพิมพ์ใหญ่ และตัวเลข 0 &&num;8211&semi; 9<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>increment&lowbar;string<&sol;strong> ใช้สำหรับสร้างสตริงที่ชื่อไม่ซ้ำกัน เช่น<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">echo increment&lowbar;string&lpar;'files'&comma;'&lowbar;'&rpar;&semi;&NewLine;echo increment&lowbar;string&lpar;'files'&comma;'&lowbar;'&rpar;&semi;&NewLine;echo increment&lowbar;string&lpar;'files'&comma;'&lowbar;'&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ เราจะได้ข้อมูลแบบสตริงที่บวกค่าไปเรื่อย คือ files&lowbar;1&comma; files&lowbar;2 และ files&lowbar;3 สามารถนำไปประยุกต์ใช้ภายใน loop ได้<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>alternator<&sol;strong> ใช้สำหรับจุดชุดข้อมูลแบบสตริงและนำมาใช้งานตามลำดับ เช่น<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">for &lpar;&dollar;i &equals; 0&semi; &dollar;i &lt&semi; 5&semi; &dollar;i&plus;&plus;&rpar;&NewLine;&lbrace;&NewLine; echo alternator&lpar;'one'&comma; 'two'&comma; 'three'&comma; 'four'&comma; 'five'&rpar;&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ ทุกครั้งที่มีการเรียกใช้งานฟังก์ชั่น ค่าที่ return กลับมาจะเป็นข้อมูลตัวถัดไปเสมอ<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>repeater<&sol;strong> ใช้สำหรับสร้างข้อมูลสตริงซ้ำกัน เช่น<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string &equals; "a"&semi;&NewLine;echo repeater&lpar;&dollar;string&comma; 30&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ จะได้ตัวอักษร a จำนวน 30 ตัวอักษรติดกัน<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>reduce&lowbar;double&lowbar;slashes<&sol;strong> ใช้สำหรับลบเครื่องหมาย &sol; &lpar;slashes&rpar; ที่ติดกัน 2 ตัว ออกไป ให้เหลือเพียง 1 ตัว<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string &equals; "http&colon;&sol;&sol;&sol;example&period;com&sol;&sol;index&period;php"&semi;&NewLine;echo reduce&lowbar;double&lowbar;slashes&lpar;&dollar;string&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ จาก url ที่ผิด เพราะมีทั้ง double slashes และ triple slashes ข้อมูลที่ได้กลับมาก็จะเป็น url ที่ถูกต้อง<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>strip&lowbar;slashes<&sol;strong> ใช้สำหรับลบเครื่องหมาย &sol; และ &bsol; ออกจาก string และ string ที่อยู่ภายใน array <&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;str <strong>&equals;<&sol;strong> <strong>array<&sol;strong>&lpar; &NewLine; 'question'  <strong>&equals;><&sol;strong> 'Is your name O&bsol;'reilly&quest;'&comma; &NewLine; 'answer' <strong>&equals;><&sol;strong> 'No&comma; my name is O&bsol;'connor&period;' &NewLine;&rpar;&semi; &NewLine;&dollar;arr &equals; strip&lowbar;slashes&lpar;&dollar;str&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ ได้ข้อมูลภายใน Array ที่ถูกลบเครื่องหมาย slashes ออกไปทั้งหมด<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>trim&lowbar;slashes<&sol;strong> ใช้สำหรับลบเครื่องหมาย &sol; ที่อยู่ด้านหน้าสุด และหลังสุดของข้อความ<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string <strong>&equals;<&sol;strong> "&sol;this&sol;that&sol;theother&sol;"&semi; &NewLine;echo trim&lowbar;slashes&lpar;&dollar;string&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ ข้อความที่คืนมาจะถูกตัดเครื่องหมาย slashes ที่อยู่ด้านหน้าและด้านหลังออก กลายเป็น <em>this&sol;that&sol;theother<&sol;em><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>reduce&lowbar;multiples<&sol;strong> ใช้สำหรับลบข้อมูลใด ๆ ก็ตามที่ซ้ำกัน<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string &equals; "aaaaa"&semi;&NewLine;echo reduce&lowbar;multiples&lpar;&dollar;string&comma;"a"&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ ค่าที่คืนกลับมาจะเหลือตัว a เพียงตัวเดียว<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>quotes&lowbar;to&lowbar;entities<&sol;strong> ใช้สำหรับแปลง single&comma; double quote back slashes kill ให้เป็นข้อมูลแสดงผลใน html <&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string &equals; "Joe's &bsol;"dinner&bsol;""&semi;&NewLine;&dollar;string &equals; quotes&lowbar;to&lowbar;entities&lpar;&dollar;string&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ <em>Joe&amp&semi;&num;39&semi;s &amp&semi;quot&semi;dinner&amp&semi;quot&semi;<&sol;em> แต่เมื่อแสดงผลใน html จะเป็นข้อความปกติแบบนี้ <em>Joe&&num;8217&semi;s &&num;8220&semi;dinner&&num;8221&semi;<&sol;em><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div style&equals;"height&colon;20px" aria-hidden&equals;"true" class&equals;"wp-block-spacer"><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>คำสั่ง <strong>strip&lowbar;quotes<&sol;strong> ใช้สำหรับลบ single และ double quote ในสตริง<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code lang&equals;"php" class&equals;"language-php">&dollar;string <strong>&equals;<&sol;strong> "Joe's &bsol;"dinner&bsol;""&semi; &NewLine;&dollar;string <strong>&equals;<&sol;strong> strip&lowbar;quotes&lpar;&dollar;string&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์จากตัวอย่างด้านบนคือ <em>Joes dinner<&sol;em> ซึ่งแตกต่างจากคำสั่ง <strong>quotes&lowbar;to&lowbar;entities<&sol;strong><&sol;p>&NewLine;

Exit mobile version