I needed to anonymize IPv4 and IPv6 addresses so I coded this crude solution: It works fine with full length IPv4 and IPv6 addresses like 207.142.131.005 2001:0db8:0000:08d3:0000:8a2e:0070:7344 but not with abbreviated addresses like 207.142.131.5 2001:0db8::8d3::8a2e:7:7344 I wonder if there is an elegant solution with preg_replace and some regular expression magic? Answer No conditional is necessary. You can write two patterns
Tag: preg-replace
PHP How to add plus (+) character before double quotes and words without double quotes in string [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 6 years ago. Improve this question I have below string: And I want to add plus (+) character before
wp_nav_menu filter, removing a specific link
I’m trying to remove a specific link from my wordpress menu: <li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-1 ubermenu-item-level-0 ubermenu-column …
preg_replace get each two characters from a word
Actually I’m trying to come up with a way of getting a string like “GEWO” out of the “Get Word” by using preg_replace function. The thing is there also can be a strings like “Get”, or “Get Word And another Word” so I have to get the proper string respectively. Thanks! Answer Use this: ouput: GEWOANANWO
Insert spaces into a string at each change of character
I’m trying to create a string with a blank space between all “different characters, for example: I tried the following recursive function, not knowing if this is the best way because I couldn’t find any build-in function in PHP for this. For some reason, this function seems to iterate infinitely, and I can’t figure out why. Where do I go
Deleting text between two strings in php using preg_replace
I’ve been trying to remove a section of text from a string the sites betweem two tags. For example: This is CROPSTART not very CROPEND cool. …should become this… This is cool. This is the PHP …
Regex to match a word, even is there are spaces between letters
I’d like to have a regex to match a word, even if there are spaces between the characters. When I want to match the word test, it should match the following: test t est t e s t And so on, but it should not match things like this: tste te ts s tet I have this regex: (t[s]*e[s]*s[s]*t[s]*) But
How to redact sensitive substring following a specific substring?
I’ve inherited this script where some sensitive information is stored in the database…and I’m wanting to replace it with ******** before it gets saved and presented in a log. I’m using PHP…and the …
Warning: preg_replace(): Unknown modifier
I have the following error: Warning: preg_replace(): Unknown modifier ‘]’ in xxx.php on line 38 This is the code on line 38: How can I fix this problem? Answer Why the error occurs In PHP, a regular expression needs to be enclosed within a pair of delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character; /, #, ~ are
Find and replace (part of) string in comment blocks with regex
I’m trying to find a certain string that can occur inside a comment block. That string can be a word, but it can also be part of a word. For instance, suppose I’m looking for the word “codex”, then this word should be replace with “bindex” but even when it’s part of a word, like “codexing”. This should be changed