ball6847
เจ้าพ่อบอร์ดเสียว
พลังน้ำใจ: 212
ออฟไลน์
กระทู้: 4,152
|
 |
« เมื่อ: 13 กันยายน 2012, 13:21:26 » |
|
วันนี้มาอับเดทใหม่ ของเก่ามันกาก มีเหตุให้ต้องเขียนใหม่ คราวนี้มาเป็น OOP ละ สั้นๆเหมือนเดิมไม่ไหวกากเกิน ยังไม่ได้เขียน doc comment ขออัมภัย เวลาบ่มี <?php
class Spinner { protected $vars = array(); protected $regex = '~\{(((?>[^{}]+)|(?R))*)\}~s'; protected $_ldelim = '{'; protected $_rdelim = '}'; public function __construct($config = array()) { if ( ! empty($config)) { $this->initialize($config); } } public function initialize($config) { foreach ($config as $name => $value) { if (isset($this->{'_'.$name})) { $this->{'_'.$name} = $value; } } } public function set($name, $value = NULL) { if (is_array($name)) { foreach ($name as $key => $value) { $this->vars[$this->_to_var($key)] = (string) $value; } } else { $this->vars[$this->_to_var($name)] = (string) $value; } } public function get($name = NULL) { if (is_null($name)) { return $this->vars; } $name = $this->_to_var($name); if (isset($this->vars[$name])) { return $this->vars[$name]; } } public function spin($output, $vars = array()) { // local variables $_vars = array(); // rebuild its keys as variable foreach ($vars as $name => $value) { $_vars[$this->_to_var($name)] = (string) $value; } // we don't need it anymore unset($vars); // now override global variables with local $_vars = array_merge($this->vars, $_vars); // start variable processing $output = str_replace(array_keys($_vars), $_vars, $output); return $this->_spin($output); } protected function _spin($output) { return preg_replace_callback($this->regex, array($this, '_spin_callback'), $output); } protected function _spin_callback($match) { if (preg_match($this->regex, $match[1])) { $match[1] = $this->_spin($match[1]); } $words = explode('|', $match[1]); return $words[array_rand($words)]; } protected function _to_var($name) { return $this->_ldelim.$name.$this->_rdelim; } }
/* End spinner.php */ Usage <?php
require('spinner.php');
$spinner = new Spinner();
// it supports variable, just set and place in your template. eg {varname} $spinner->set('varname1', 'addicted to ThaiSEOBoard');
// this is raw text you want to spin, supports variable and nested spin block $template = "{{You should not|You shouldn't|You ought not|It's not necassary to}|{You shouldn't|You should not|You mustn't|You must not}|{You ought not|It's not necassary to|You must not}|{It's not necassary to|You shouldn't|You cannot|You ought not}} {varname1}";
// call spin() method when you are ready echo $spinner->spin($template);
/* End example.php */
Output จะออกมาได้ประมาณ "You should not addicted to ThaiSEOBoard" Features Caution! Use it on your own risk ...  - แม่มแดรกแรมฝุดๆ ต้อง process ทุกบล็อค ยิ่งสปินเยอะหรือซ้อนเยอะยิ่งหนัก ... ระวังโดนฟรีคิ๊ก
- ระวัง อย่าเอา javascript กับ css มาใส่รวมในนี้ ผมไม่ได้เขียน protect ให้นะ
|
|
|
|
« แก้ไขครั้งสุดท้าย: 28 กรกฎาคม 2013, 06:21:22 โดย ball6847 »
|
บันทึกการเข้า
|
|
|
|
The_Lakers
คนรักเสียว
พลังน้ำใจ: 7
ออฟไลน์
กระทู้: 143
|
 |
« ตอบ #1 เมื่อ: 13 กันยายน 2012, 13:39:18 » |
|
เขียนฟังก์ชั่นมาแจกเล่น แก้เซ้ง  <?php
/** * Callback function for preg_replace_callback * Randomly return one word in list * * @param Array $match matched result from preg_replace_callback pattern * @return String random word in list */ function spin_replace_callback($match) { $words = explode('|', $match[1]); return $words[array_rand($words)]; }
/** * Process each spin block in string * * each block must start with "{" and end with "}" * separate each word with "|" * * eg. {hello|hi} {everyone|everybody} * * @param String $text string to process spin text block * @return String spin result */ function process_spin_block($text) { return preg_replace_callback('~\{(.*?)\}~', 'spin_replace_callback', $text); }
วิธีใช้ <?php
echo process_spin_block('{Hello|Hi} {everyone|everybody}. Do you know {who|what} am i ?');
สั้นๆแต่ใช้งานได้จริง
|
|
|
|
|
บันทึกการเข้า
|
Account Closed ไม่ใช่ปัญหา - Aff ไม่ได้มีแค่ Amazon 
|
|
|
WPDSign
Verified Seller
เจ้าพ่อบอร์ดเสียว
พลังน้ำใจ: 500
ออฟไลน์
กระทู้: 9,055
|
 |
« ตอบ #2 เมื่อ: 13 กันยายน 2012, 13:50:30 » |
|
เคยคิดจะทำเหมือนกัน แต่ยังคิดไม่ออก
ขอบคุณครับ thank เลย
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
pkake2000
ก๊วนเสียว
พลังน้ำใจ: 103
ออฟไลน์
กระทู้: 494
|
 |
« ตอบ #3 เมื่อ: 13 กันยายน 2012, 13:59:24 » |
|
ง่ายอย่างนี้เลยหรอ ขอบคุณที่เอามาแชร์ครับ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
mymo
เจ้าพ่อบอร์ดเสียว
พลังน้ำใจ: 302
ออฟไลน์
กระทู้: 3,161
|
 |
« ตอบ #4 เมื่อ: 13 กันยายน 2012, 14:07:41 » |
|
ขอบคุณครับ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
Thong K.
คนรักเสียว
พลังน้ำใจ: 7
ออฟไลน์
กระทู้: 186
|
 |
« ตอบ #5 เมื่อ: 13 กันยายน 2012, 14:18:57 » |
|
ขอบคุณครับ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
nst_nk
Verified Seller
เจ้าพ่อบอร์ดเสียว
พลังน้ำใจ: 385
ออฟไลน์
กระทู้: 3,391
|
 |
« ตอบ #6 เมื่อ: 13 กันยายน 2012, 14:24:41 » |
|
 ขอบคุณครับ
|
|
|
|
|
บันทึกการเข้า
|
◤◤ รับเขียนระบบต่างๆครับ เช่น ระบบ Shipping , ร้านค้าออนไลน์ , อสังหาริมทรัพย์ , ระบบจัดการหลังบ้านทุกประเภท สอบถามได้ครับ Line : bigznst ◥◥
|
|
|
อัลฟา
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 125
ออฟไลน์
กระทู้: 1,744
|
 |
« ตอบ #7 เมื่อ: 13 กันยายน 2012, 14:26:39 » |
|
ขอบคุณค่ะ
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
chaip
คนรักเสียว
พลังน้ำใจ: 7
ออฟไลน์
กระทู้: 177
|
 |
« ตอบ #8 เมื่อ: 13 กันยายน 2012, 14:28:07 » |
|
ขอบคุณครับ ต้องลอง
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
batdboyz
Verified Seller
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 177
ออฟไลน์
กระทู้: 1,542
|
 |
« ตอบ #9 เมื่อ: 13 กันยายน 2012, 14:39:20 » |
|
เจ๋งเลย สั้นๆ :")
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
hyde4fun
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 129
ออฟไลน์
กระทู้: 1,837
|
 |
« ตอบ #10 เมื่อ: 13 กันยายน 2012, 14:45:27 » |
|
ขอบคุณครับ +1 ให้เลย
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
flash
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 16
ออฟไลน์
กระทู้: 1,022
|
 |
« ตอบ #11 เมื่อ: 13 กันยายน 2012, 14:46:16 » |
|
สั้นได้ใจ ขอบคุณครับ
|
|
|
|
|
บันทึกการเข้า
|
ครีมหมอจุฬา  ผ่าน อย. อยากหน้าใส ไร้รอยด่างดำ ครีมหมอจุฬาซื้อที่ไหน หาไม่ยากเลย แต่ระวังของปลอมนะ ของเราเป็น ครีมหมอจุฬาของแท้ 100% มาดูสินค้าก่อนได้ 
|
|
|
adidog
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 238
ออฟไลน์
กระทู้: 1,858
|
 |
« ตอบ #12 เมื่อ: 13 กันยายน 2012, 15:49:45 » |
|
เขียนฟังก์ชั่นมาแจกเล่น แก้เซ้ง  <?php
/** * Callback function for preg_replace_callback * Randomly return one word in list * * @param Array $match matched result from preg_replace_callback pattern * @return String random word in list */ function spin_replace_callback($match) { $words = explode('|', $match[1]); return $words[array_rand($words)]; }
/** * Process each spin block in string * * each block must start with "{" and end with "}" * separate each word with "|" * * eg. {hello|hi} {everyone|everybody} * * @param String $text string to process spin text block * @return String spin result */ function process_spin_block($text) { return preg_replace_callback('~\{(.*?)\}~', 'spin_replace_callback', $text); }
วิธีใช้ <?php
echo process_spin_block('{Hello|Hi} {everyone|everybody}. Do you know {who|what} am i ?');
ขอเก็บเข้าคลังแสง +1 งามๆครับ
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
Ferrited
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 159
ออฟไลน์
กระทู้: 1,898
|
 |
« ตอบ #13 เมื่อ: 13 กันยายน 2012, 17:38:49 » |
|
ขอบคุณค่ะ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
system-4x
ก๊วนเสียว
พลังน้ำใจ: 16
ออฟไลน์
กระทู้: 259
|
 |
« ตอบ #14 เมื่อ: 13 กันยายน 2012, 18:20:16 » |
|
ขอบคุณครับ แหล่มเลย 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
nineaom
หัวหน้าแก๊งเสียว
พลังน้ำใจ: 100
ออฟไลน์
กระทู้: 1,463
|
 |
« ตอบ #15 เมื่อ: 10 ตุลาคม 2012, 20:01:07 » |
|
เอาไปใช้ไม่เป็น แหะๆ
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
tonnant
Verified Seller
ก๊วนเสียว
พลังน้ำใจ: 102
ออฟไลน์
กระทู้: 292
|
 |
« ตอบ #16 เมื่อ: 10 ตุลาคม 2012, 20:38:05 » |
|
ขอบคุณครับ
|
|
|
|
|
บันทึกการเข้า
|
รับเขียน imacro javascript รับทำเว็บไซต์ php
|
|
|
reactionkz
ก๊วนเสียว
พลังน้ำใจ: 18
ออฟไลน์
กระทู้: 359
|
 |
« ตอบ #17 เมื่อ: 10 ตุลาคม 2012, 20:46:59 » |
|
-ขอบคุณคราบบ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
ayeweb
Verified Seller
เจ้าพ่อบอร์ดเสียว
พลังน้ำใจ: 321
ออฟไลน์
กระทู้: 5,202
|
 |
« ตอบ #18 เมื่อ: 10 ตุลาคม 2012, 20:51:12 » |
|
น้ำใจงามครับ 
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
00benza00
คนรักเสียว
พลังน้ำใจ: 7
ออฟไลน์
กระทู้: 120
|
 |
« ตอบ #19 เมื่อ: 10 ตุลาคม 2012, 22:08:31 » |
|
ขอบคุณมากเลยครับ
|
|
|
|
|
บันทึกการเข้า
|
|
|
|
|