how to keep special characters using preg_replace in php [duplicate] - php

This question already has answers here:
Is There a Way to Match Any Unicode Alphabetic Character?
(2 answers)
Closed 4 years ago.
I am trying to use preg_replace function and it works as expected. The problem is it also removes the special alphabet like this one Ö and removes O. How can I keep Ö?
$string='GÖTEBORG-SEASON-1';
echo $str=preg_replace('/[^A-Za-z-_]/', '', $string);
It output GTEBORG-SEASON- (Ö is missing) but I am expecting GÖTEBORG-SEASON-
Thank you.

I think I have solved it. I need to use something like this
preg_replace ('/[^\p{L}-_]/u','', $string);

Related

Regex PHP part of string [duplicate]

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

Why Chinese character performance wrong in preg_replace function? [duplicate]

This question already has answers here:
Matching Unicode letter characters in PCRE/PHP
(5 answers)
Closed 5 years ago.
My code is:
preg_replace('/[中]/', '1', '中,博文大,精中深');
Why the result is:
111,博文大,精111深
The Chinese character '中' should be replace once, while triple instead.
Any help? Thanks
First of all, please read this article about unicode characters in regexps.
Next, you may need this article about modifiers. I think that you need u modifier in your regexp.
preg_replace('/[中]/u', '1', '中,博文大,精中深');
Please, also read comments in modifiers article for more examples.
Also, for simple replaces like in example above you can use str_replace.
str_replace('中', '1', '中,博文大,精中深');

php preg_replace: Replace word, even with special chars in it [duplicate]

This question already has answers here:
How to I remove special characters and spaces on a textfield using PHP
(3 answers)
Closed 6 years ago.
I need to find and replace a word like "test", but even if it has none-alphabetic chars in it, like:
test
t e s t
t.e.s.t
t-e-s-t
t.e-s(t
etc.
Any ideas how to do this in php? Perhaps with preg_replace?
Try this:
preg_replace("/t\W?e\W?s\W?t/", "something", $input_array);

How to remove a whitespace in php even if its not detected [duplicate]

This question already has answers here:
How do I strip all spaces out of a string in PHP? [duplicate]
(4 answers)
Closed 7 years ago.
I'm trying to remove a whitespace from a string, but this whitespace its not detected, i've tried so many solutions like str_replace(), preg_replace() htmlentities() an html_entity_decode() but its not working because i can't detect this whitespace even if it's really present.
So any ideas please ?
An example : 7412 148 ==> The goal is : 7412148
PS : i've tried to convert it to INT (not working) !
Thanks
Let's use preg_replace to replace/remove everything that is not a digit.
$str = "1234 5657"; // this is an example
$number = preg_replace("/[^0-9]/","",$str);
print $number;
Use this with the string that your function returns and let us know if it works.

PHP preg_replace only muptiple occurrences [duplicate]

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.

Categories