แจกเล่น php function สำหรับ spin ข้อความ

เริ่มโดย ball6847, 13 กันยายน 2012, 13:21:26

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

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

ball6847

วันนี้มาอับเดทใหม่ ของเก่ามันกาก มีเหตุให้ต้องเขียนใหม่ คราวนี้มาเป็น OOP ละ สั้นๆเหมือนเดิมไม่ไหวกากเกิน

ยังไม่ได้เขียน doc comment ขออัมภัย เวลาบ่มี

โค้ด (spinner.php) เลือก
<?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

โค้ด (example.php) เลือก
<?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

  • spin ซ้อน spin
  • ตัวแปร

Caution! Use it on your own risk ...  :o

  • แม่มแดรกแรมฝุดๆ ต้อง process ทุกบล็อค ยิ่งสปินเยอะหรือซ้อนเยอะยิ่งหนัก ... ระวังโดนฟรีคิ๊ก
  • ระวัง อย่าเอา javascript กับ css มาใส่รวมในนี้ ผมไม่ได้เขียน protect ให้นะ
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

The_Lakers

อ้างถึงจาก: ball6847 ใน 13 กันยายน 2012, 13:21:26
เขียนฟังก์ชั่นมาแจกเล่น แก้เซ้ง  :wanwan044:

<?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

เคยคิดจะทำเหมือนกัน แต่ยังคิดไม่ออก

ขอบคุณครับ thank เลย
สร้างบ้าน Modern [direct=https://casa-concept1.com/]แบบบ้านโมเดิร์น[/direct] มาแรง
» ต้องการสร้างบ้านดูแบบบ้านสวยๆ [direct=http://www.architect-bkk.com/แบบบ้านสองชั้น]แบบบ้านสองชั้น[/direct] ทุกสไลต์บริการ[direct=http://www.ideaican.com/]รับสร้างบ้าน[/direct] ใครฝันอย่างสร้างบ้านสไตล์รีสอร์ท มี[direct=https://แบบบ้านสไตล์รีสอร์ท.blogspot.com]แบบบ้านสไตล์รีสอร์ท[/direct]พร้อมก่อสร้าง ลงประกาศขายบ้านที่ [direct=http://www.classified108.com/]ลงประกาศ[/direct]

pkake2000

ง่ายอย่างนี้เลยหรอ ขอบคุณที่เอามาแชร์ครับ  :wanwan003:

mymo


Thong K.


nst_nk


  ◤◤ รับเขียนระบบต่างๆครับ เช่น ระบบ Shipping , ร้านค้าออนไลน์ , อสังหาริมทรัพย์ , ระบบจัดการหลังบ้านทุกประเภท  สอบถามได้ครับ Line : bigznst ◥◥

อัลฟา

[direct=http://x.co/1XLR7]฿ ใครกำลังมองหา Domain ราคาถูก!! ของ Goddady ในราคาเพียงแค่ 179.7 บาทเท่านั้นค่ะ คลิกเลย (:[/direct]
@----------------------------------------------------------------------@
฿รับสมัคร Google Adsense สมัครแบบผ่าน 100% และ Acc ของมีคุณภาพด้วย รับประกันคุณภาพค่ะ ตอนนี้รับอีกแค่ 1 Acc เท่านั้นนะค่ะ มีรายงานการทำงานให้ดูด้วยค่ะPM!! (:


batdboyz


hyde4fun

[direct=https://www.seono1.co.th]รับทำ SEO[/direct] ติดอันดับ Google พร้อมผลงาน No.1 จริงมากมาย ขอดูผลงานได้
[direct=https://www.facebook.com/SEONo1coth]รับทำ SEO[/direct] ขาย Backlink PBN DA PA สูง
[direct=https://twitter.com/SeoNo1coth]รับทำ SEO สายขาว[/direct] ด้วยเทคนิค [direct=https://www.seono1.co.th]SEO[/direct] No.1

ติดต่อ[direct=https://bit.ly/seoNo1]รับทำ SEO[/direct] ทาง Line id: @SEONo1

flash

[direct=http://www.mclshop.com/]ครีมหมอจุฬา[/direct] ผ่าน อย. อยากหน้าใส ไร้รอยด่างดำ ครีมหมอจุฬาซื้อที่ไหน หาไม่ยากเลย แต่ระวังของปลอมนะ ของเราเป็น ครีมหมอจุฬาของแท้ 100% มาดูสินค้าก่อนได้


adidog

อ้างถึงจาก: ball6847 ใน 13 กันยายน 2012, 13:21:26
เขียนฟังก์ชั่นมาแจกเล่น แก้เซ้ง  :wanwan044:

<?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 งามๆครับ
Certain Dri [direct=http://www.ishiclub.com/]Certain Dri[/direct]
คุณแม่มือใหม่ [direct=http://www.mamyget.com/]คุณแม่มือใหม่[/direct]

Ferrited

คูปองลด Hostgator "gt25percentoff"
[direct=http://jigload.com]แอนดรอยด์[/direct]
อัพเดทข่าววงการไอที ตลอด 24 ชั่วโมง
ทุกเรื่องราวเกี่ยวกับ[direct=http://jigload.com]ไอโฟน[/direct][direct=http://pirun.ku.ac.th/~g5614500537]ธีมไลน์ [/direct]([direct=http://jigload.com]ซัมซุง กาแล็กซี่[/direct])และ[direct=http://jigload.com]โปรแกรมแต่งรูป[/direct])

system-4x

[direct=http://www.system-4x.com/board]Naruto One Piece Bleach ใหม่ล่าสุด[/direct][direct=http://www.sakidkao.com]สะกิดข่าว[/direct][direct=http://www.system-4x.com/board/index.php/board,47.0.html]ลงประกาศฟรี[/direct]

nineaom

รับปั่นอเมซอน เกรด A+ ราคาเว็บละ 500 ถูกม๊วกก Im มาเลยครับ
[direct=http://www.leadsmarket.com/?c=15&v1=12137][/direct]
ขาย Amazon Account ราคา 1500 บาท
[direct=http://bestgaminglaptopof2014.com]Best gaming laptop of 2014[/direct]

แอดมาเลย fb.com/Omsuperman

tonnant

รับเขียน imacro javascript รับทำเว็บไซต์ php

reactionkz


ayeweb


[direct=https://www.bitimer.in.th/]ข่าวไอที[/direct] ข่าวสารไอที แอพไอโฟน บริการอื่นๆ
[direct=https://page.line.me/gnm7628z]บริการ ตรวจหวย[/direct] อื่นๆ ทั่วไปสาระน่ารู้ ความรู้ต่างๆ
[direct=https://goo.gl/XQp91t]Host ไทย[/direct] เว็บผู้หญิงนะคะ แต่ผู้ชายก็เข้าได้ค่ะ
[direct=https://page.line.me/oer1981h]ตรวจหวย[/direct]
ตรวจหวย ผลสลากกินแบ่งรัฐบาล>>
[direct=https://goo.gl/H3JCzK]เช่าโฮสติ้ง Ruk-com[/direct]

00benza00