2014-02-17 52 views
-1

我们有动态的帖子,我们应该在URL转换阵列串(分3个字)

$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); 
     } 

分割字符串的3个字的使用,这是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 IS SAMPLE.ht毫升

+0

你想要得到的'$这个 - > post_message'的第三个字?只是澄清.. – Vainglory07

+0

是的,我想要得到$ this-> post_message的第一个三字! – user3261715

回答

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" 
0

也许有些正则表达式向导可以做得比这更好,但:

$this->post_message = "THIS IS SAMPLE STRING FOR TEST"; 
$result = preg_match("/^([^ ]+(?: [^ ]+)?(?: [^ ]+)?)/", $this->post_message, $matches); 
if ($result) { 
    $bodypost = $matches[1]; 
}