2014-10-10 40 views
-5

如果我echo $test ='test text'它在我的網站上顯示爲<i>test text</i>。我如何使用str_replace或其他內容,以便輸出只顯示該字符串的前3個字母?Str替換php mysql

+2

RTFM:http://php.net/substr – 2014-10-10 19:14:50

+0

^^^^^會給你鏈接到PHP.net,你有Google'd「顯示第一個字母的PHP」 – 2014-10-10 19:15:32

+2

這個問題似乎是脫離主題,因爲它可以通過基本的網頁搜索來回答。 – Mooseman 2014-10-10 19:25:46

回答

3
echo substr($test, 0, 3); 

子串是您正在尋找的術語。

3

試試這個:

<?php 

    $test = "test test"; 
    echo substr($test , 0, 3); 

?> 
0

試試這個

echo substr($test , 0, 3); 
0

如下您可以使用SUBSTR()函數:

$test="test text"; 
echo substr($test,0,2); 
0

要回答你的問題多一點字面上:

//set string to variable 
$test = "test text"; 

// extract only the alphbet chars 
$test = preg_replace("/[^a-zA-Z0-9]+/", "", $test); 

//then do the substr 
$test = substr($test , 0, 3); 

echo $test;