PHP preg_replace only muptiple occurrences [duplicate] - php

This question already has answers here:
PHP Preg-Replace more than one underscore
(7 answers)
Closed 1 year ago.
this following code will replaces spaces correctly:
$string = preg_replace("/[[:blank:]]+/", "", $string);
but how can I make it so that it will only replace it if there is more than 2 blank spaces? Because right now it replaces all spaces, I only need it to replace more than one space. I searched on here and see people use totally different preg_replace codes, but it also removes newlines so if the code I posted can just be simply modified to allow more than one blank, that would be great. I remember a while back reading a tutorial where it used something like {2+} in the preg area to match anything with more than two or something but not sure how to make it work correctly.

/[[:blank:]]{2,}/
That will make it replace sequences of two or more.
The php manual has a chapter about repetition/quantifiers.

$string = preg_replace("/[[:blank:]]+/", " ", $string);
Same as yours but replaces all occurrences of spaces with one space.

Related

How to remove 3 slashes in a row in a string with str_replace [duplicate]

This question already has answers here:
How to replace "\" using str_replace() in PHP?
(5 answers)
Closed 3 years ago.
I have a script where I grab the body of an email and put that into a database. For whatever reason once in a while I will get an email wherein the string I have 3 slashes in a row within a sentence like so
$string = "You\\\'ll be great at that one day";
I am trying to figure out with str_replace how to remove the 3 slashes. If I do
str_replace("\\\","",$string)
of course, it doesn't work. How can I specifically target 3 slashes with str_replace?
SOLVED
With much messing around a simple str_replace("\\\'", "'", $string) did the trick.
There is a problem with your input string. If you want three literal backslashes in a PHP string, then you need six backslashes, because \\ is a literal backslash. Consider the following script:
$string = "You\\\\\\'ll be great at that one day";
echo $string . "\n";
echo str_replace("\\\\\\", "", $string);
This prints:
You\\\'ll be great at that one day
You'll be great at that one day
Note that you could also do a regex replacement with preg_replace, targeting exactly three backslashes:
echo preg_replace("/\\\\{3}/","",$string);
In regex terms, a single literal backslash is actually represented by four backslashes.

What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space? [duplicate]

This question already has answers here:
How do I replace tabs with spaces within variables in PHP?
(9 answers)
Replace all occurences of char inside quotes on PHP
(5 answers)
Closed 5 years ago.
I know what needs to be done, but have been unsuccessful with the correct regex.
What is the regular expression I should use in preg_replace() to find tabs inside all quoted strings and replace with a single space?
any help would be much appreciated.
Example:
$string = '"Foo\tMan\tChoo"'
preg_replace($expression_string, ' ',$string);
echo $string;//desired result---->'"Foo Man Choo"'
I think this question is not a duplicate
answer : you will need to repeat preg_replace as many time as the max of \t you expect in a field (never could be bothered comming up with a more generic solution : it is usually possible to use this one)
also make sure every line ends with a tab (otherwise the last field will not be processed)
then you need to repeat replacement starting with the max possible number of \t (2 in the example)
$string = '"Foo'."\t".'Man'."\t".'Choo"'."\t".'boo'."\t".'"Foo'."\t".'Man"'."\t";
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2_$3"'."\t",$string);
$string = preg_replace('/"([^"]*)'."\t".'([^"]*)"'."\t".'/','"$1_$2"'."\t",$string);
echo $string;

How to prevent zalgo text using php [duplicate]

This question already has answers here:
Remove special characters that mess with formating
(2 answers)
Closed 7 years ago.
I have some problems with Zalgo on my imageboard.
Texts like below mess up my imageboard. Is there a way to prevent these characters and "fix" or clean up the texts?
Example text Source:
ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ
I tried to use this solution:
$cleanMessage = preg_replace("/[^\x20-\xAD\x7F]/", "", $input_lines);
Taken from here: Remove special characters that mess with formating
But it works only for latin chars
Can anyone help me?
This regular expression replaces every superscript symbol in the $text variable:
$text = preg_replace("~[\p{M}]~uis","", $text);
If $text contains char with superscript, for example กิ this regex will remove that superscript symbol and result $text will contain just ก.
I was improved this regex and changed it to filter only second level of phonetic marks
$text = preg_replace("~(?:[\p{M}]{1})([\p{M}])+?~uis","", $text);
This regex will filter only second level of superscript symbols.
Use it if you want to filter deutch or other languages with reserved marks.
This regex will transform this word -
͐̈ͩ̎Zͮ͌ͦ͆ͦͤÃ̉͛̄ͭ̈̚LͫG̉̋͂̉Oͨ͌̋͗!
into this: ZÄLͫGO!
I hope second regex will help you.

Preg_replace with multiple lines(php) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace text over multiple lines using preg_replace
I am trying to read some text from a file and than replacing some pattern. If I try to replace the patern from just a single string it works, but if there are multiple such strings in a file it doesnt work.
$this->session->set_flashdata('error_message', 'Naslovna vrstica je bila uspešno shranjena');
This is an example of text that I am trying to replace the replacment works ok with just this line, but not if there are other such lines in a file, which all match individualy though.
$content = file_get_contents("C:\Users\Borut\\test.txt");
$pattern="/^.*session->set_flashdata\((.*),(.*)\);$/";
$replacement="\$_SESSION[$1]=$2";
this is my code. How do you replace multiple strings as the one shown above.
The modifier you want is m. You can find all modifiers here
That said, the easiest and better regex solution would be
"/\$this->session->set_flashdata\((.*?),\s*(.*?)\);/"
Notice how there's a ? after the .* in each. This is to stop greedy matching as with yours. Also notice that the modifier isn't required either with the removal of the ^ and $

Replace content between two words [duplicate]

This question already has answers here:
Get content between two strings PHP
(7 answers)
Closed 4 years ago.
I am trying to replace the content between two words using php. The content between the two words is different so I can't use tradition str_replace. I want to replace the content between two words for example:
I would like to replace **some string of text** between two words
change to:
I would like to replace between two words
You can see that I removed all the wording between "some" and "text". Again I cannot use regular str_replace because the text between the two words may differ. For example it may say:
I would like to replace **some words of text** between two words
change to:
I would like to replace between two words
The regex is simple: /some .*? text/
Just replace it with the empty string.
According to your question, only the inner part of your string changes. If that is the case it's rather trivial, because you already have the solution: You do not need to replace it, but you just need to not take it over:
$result = substr($string, 0, $startlen) . substr($string, -$endlen);
Probably this helps you to find some more "resolution angles" for such problems.

Categories