2014-10-10 69 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;