Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

php - Sub-string without counting blank spaces

I want to make a sub-string, where the $count only counts letters, not spaces. This is what I have so far:

$string ="vikas tyagi php";
$string = substr($string, 0, 10);
echo $string;

Output:

vikas tyag

Desired output (I don't want to count the spaces):

vikas tyagi

How would I do this?

i want extract string with those condition

1)Base on count letter

2)Without white space

3)String limit also

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply count the spaces and add them to the desired length of the capture:

function spaceless_substr($string, $start, $count) {
    return substr($string, $start, ($count+substr_count($string, ' ', $start, $count)));
}

$string ="vikas tyagi asd sd as asd";
echo substr($string, 0, 14);
// return: "vikas tyagi a"
echo spaceless_substr($string, 0, 14);
// return: "vikas tyagi asd" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...