问题
How to hide a particular post with PHP "preg_replace" or shorter code?
I want to show the first 5 and last 5 digits after pub-
of the specified text. The string always starts with pub-
and there are always 16 numbers in the sequence. Only the numbers change. I want to replace the middle 6 digits with asterisks.
$string = "pub-9752787982789258";
echo "pub-" . substr(explode("-", $string)[1], 0, 5) . '******' . substr(explode("-", $string)[1], 11, 5);
RESULT:
pub-97527******89258
回答1:
If you need to hide the substring in the middle only in strings that match the specific format you may use
preg_replace('~(?:\G(?!^)|^pub-\d{5}(?=\d*$))\K\d(?=\d{5})~', '*', $string)
See the PHP demo and a regex demo.
Details
(?:\G(?!^)|^pub-\d{5}(?=\d*$))
- end of the previous successful match orpub-
at the start of the string and then 5 digits followed with any 0+ digits up to the end of the string and then\K
- omits the text matched so far\d
- matches any 1 digit(?=\d{5})
- makes sure there are five digits on the right.
回答2:
Have you try this kind of approach:
echo substr_replace($string, '******', 9, strlen($string) -14 );
回答3:
Given that the string width be fixed, I might suggest avoiding regex here and instead just use the base string functions:
$string = "pub-1885335825402893";
$output = substr($string, 0, 9) . "******" . substr($string, 15, 5);
echo $output;
pub-18853******02893
回答4:
Because the input is strictly formatted and has a static length, there is no need for a regex call and this can be done with just one function call (making it easier to read and maintain and more efficient).
Count to the correct position in the, either from the front with a positive integer or from the back with a negative integer, then set the replacement length equal to the number of asterisks.
My preg_replace()
call is the longest and least efficient technique. It consumes and forgets the first 5 digits, then replaces the next 6 digits.
To make individual replacements, the final two techniques (regex with \G
continue metacharacter or for
loop) will do the job. The regex will be even less efficient than the earlier regex. The for()
loop may be competitively fast, but I don't really see the benefit in making 6 separate replacements for your scenario.
Code: (Demo)
$string = "pub-1885335825402893";
echo substr_replace($string, '******', -11, 6);
echo "\n---\n";
echo substr_replace($string, '******', -11, -5);
echo "\n---\n";
echo substr_replace($string, '******', 9, 6);
echo "\n---\n";
echo substr_replace($string, '******', 9, -5);
echo "\n---\n";
echo preg_replace('~\d{5}\K\d{6}~', '******', $string);
echo "\n---\n";
echo preg_replace('~(?:\G(?!^)|^pub-\d{5})\K\d(?=\d{5})~', '*', $string);
echo "\n---\n";
for ($offset = 9; $offset <= 14; ++$offset) {
$string[$offset] = '*';
}
echo $string;
Output:
pub-18853******02893
---
pub-18853******02893
---
pub-18853******02893
---
pub-18853******02893
---
pub-18853******02893
---
pub-18853******02893
---
pub-18853******02893
来源:https://stackoverflow.com/questions/58703110/how-to-hide-a-particular-post-with-preg-replace-or-shorter-code