Home  >  Article  >  Backend Development  >  How to replace the middle character of a string with an ellipsis in PHP_PHP Tutorial

How to replace the middle character of a string with an ellipsis in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:03830browse

How to replace the middle character of a string with an ellipsis in php

This example describes how to replace the middle character of a string with an ellipse in php. Share it with everyone for your reference. The specific analysis is as follows:

For a long string, if you only want the user to see the beginning and end of the content and hide the middle content, you can use this php function, which can specify the number of middle strings to be hidden

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

/**

* Reduce a string by the middle, keeps whole words together

*

* @param string $string

* @param int $max (default 50)

* @param string $replacement (default [...])

* @return string

* @author david at ethinkn dot com

* @author loic at xhtml dot ne

* @author arne dot hartherz at gmx dot net

*/

function strMiddleReduceWordSensitive($string,$max=50,$rep='[...]'){

$strlen = strlen($string);

if ($strlen <= $max)

return $string;

$lengthtokeep = $max - strlen($rep);

$start = 0;

$end = 0;

if (($lengthtokeep % 2) == 0) {

$start = $lengthtokeep / 2;

$end = $start;

} else {

$start = intval($lengthtokeep / 2);

$end = $start 1;

}

$i = $start;

$tmp_string = $string;

while ($i < $strlen) {

if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {

$tmp_string = substr($tmp_string, 0, $i) . $rep;

$return = $tmp_string;

}

$i ;

}

$i = $end;

$tmp_string = strrev ($string);

while ($i < $strlen) {

if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {

$tmp_string = substr($tmp_string, 0, $i);

$return .= strrev ($tmp_string);

}

$i ;

}

return $return;

return substr($string, 0, $start).$rep.substr($string, - $end);

}

1

2

3

1

2

3

4

5

6

// example:

$text = 'This is a very long test sentence, bla foo bar nothing';

print strMiddleReduceWordSensitive ($text, 30) . "n";

// Returns: This is a very[...]foo bar nothing (~ 30 chrs)

print strMiddleReduceWordSensitive ($text, 30, '...') . "n";

// Returns: This is a very...foo bar nothing (~ 30 chrs)

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/** * Reduce a string by the middle, keeps whole words together * * @param string $string * @param int $max (default 50) * @param string $replacement (default [...]) * @return string * @author david at ethinkn dot com * @author loic at xhtml dot ne * @author arne dot hartherz at gmx dot net */ function strMiddleReduceWordSensitive($string,$max=50,$rep='[...]'){ $strlen = strlen($string); if ($strlen <= $max) return $string; $lengthtkeep = $max - strlen($rep); $start = 0; $end = 0; if (($lengthtkeep % 2) == 0) { $start = $lengthtkeep / 2; $end = $start; } else { $start = intval($lengthtkeep / 2); $end = $start 1; } $i = $start; $tmp_string = $string; while ($i < $strlen) { if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') { $tmp_string = substr($tmp_string, 0, $i) . $rep; $return = $tmp_string; } $i ; } $i = $end; $tmp_string = strrev ($string); while ($i < $strlen) { if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') { $tmp_string = substr($tmp_string, 0, $i); $return .= strrev ($tmp_string); } $i ; } return $return; return substr($string, 0, $start).$rep.substr($string, - $end); }
Demonstration example:  ?
1 2 3 4 5 6 // example: $text = 'This is a very long test sentence, bla foo bar nothing'; print strMiddleReduceWordSensitive ($text, 30) . "n"; // Returns: This is a very[...]foo bar nothing (~ 30 chrs) print strMiddleReduceWordSensitive ($text, 30, '...') . "n"; // Returns: This is a very...foo bar nothing (~ 30 chrs)

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/993429.htmlTechArticleHow to replace the middle character of a string with an ellipsis in php. This article explains how to replace the middle character of a string with an ellipse in php. . Share it with everyone for your reference. The specific analysis is as follows:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn