将数组转换为字符串(拆分3个字)

时间:2014-02-17 23:57:04

标签: php arrays explode

我们有动态帖子,我们应该分割3个字符串,以便在url中使用

$this->post_message = "THIS IS SAMPLE STRING FOR TEST";
if (strlen($this->post_message) > 5) {
        $key='<> ";,/\][{}|`~!#$%^&*_+=-';
        $bodypost1 =trim($this->post_message,$key);
        $bodypost2 = explode (' ',$bodypost1,3);
        $bodypost = array_shift(',',$bodypost2);
        }else{

        $bodypost2 = explode (' ',$this->post_message,3);
        $bodypost = array_shift(',',$bodypost2);
        }

这是URL参数

$this->permalink        = $C->SITE_URL.'view/'.($type=='private'?'priv':'post').':'.$this->post_id.'/'.$bodypost.'.html';

我的网址回复:

sitename.com/123/THIS.html
但应该是 sitename.com/123/THIS是SAMPLE.html

1 个答案:

答案 0 :(得分:1)

试试这个

$message = "THIS IS SAMPLE STRING FOR TEST";

$string = implode(' ', array_slice(explode(' ', $message), 0, 3)); 

var_dump($string);

//result ...

string(14) "THIS IS SAMPLE"