Php replaces the string with ellipsis (...)

Source: Internet
Author: User
Tags dot net
This article describes how to replace the intermediate character of a string with a ellipsis in php, which can be used to replace the intermediate part of a string with a ellipsis. It is suitable for hiding sensitive information such as accounts and mobile phone numbers, if you need a friend

This article describes how to replace the intermediate character of a string with a ellipsis in php, which can be used to replace the intermediate part of a string with a ellipsis. It is suitable for hiding sensitive information such as accounts and mobile phone numbers, if you need a friend

This example describes how to replace the ellipsis (...) in the string with the intermediate character in php. Share it with you for your reference. The specific analysis is as follows:

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

/*** 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 );}

Demo:

// 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 help you with php programming.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.