Good afternoon,
I have an error in my preg_replace. I would like to replace && with only one &, and ?& with ?.
My code looks like this:
$reg = preg_replace("#\&\&#is", "&
", $reg);
$reg = preg_replace("#\?\&#is", "?
", $reg);
Could you please help me fix this? I am sure, it some basic error, so sorry for that...
Thanks!
You do not need to escape & only the ?
$reg = preg_replace("#&&#", "&", $reg);
$reg = preg_replace("#\?&#", "?", $reg);
You can simplify the two regexs into one.
echo preg_replace("#([?&])\s*&#", "$1", ' ? &lang=en');
Output:
?lang=en
Your modifiers didn't make sense since you aren't using alpha characters or the ..
Also & isn't a special regex character, just ?. If in a character class ([]) neither will need to be replaced.
Regex101 Demo: https://regex101.com/r/iS4mQ0/1
Related
The issue:
Basically when it sees type of letter that regex don't allow it messes up with the link.
My function in php to convert the names that are read from database into links:
function convertActor($str) {
$regex = "/([a-zA-Z-.' ])+/";
$str = preg_replace($regex, "<a href='/pretraga.php?q=$0' title='$0' class='actor'>$0</a>", $str);
return $str;
}
Also I want to allow spaces, dashes, dots and single quotes.
Thanks in advance.
You could try this:
$regex = "/(?:[a-zA-Z.\-' ]|[^\\u0000-\\u007F,])+/";
Which converts régime, Coffehouse Coder, Apple into
'<a href='/pretraga.php?q=régime' title='régime' class='actor'>régime</a>,<a href='/pretraga.php?q= Coffehouse Coder' title=' Coffehouse Coder' class='actor'> Coffehouse Coder</a>,<a href='/pretraga.php?q= Apple' title=' Apple' class='actor'> Apple</a>',
here on regex101.com.
The follwing regex should work for you
[^\u0000-\u007F ]|[a-zA-Z-.' ]\g
[^\u0000-\u007F ] : will match all non English Unicode characters.
[a-zA-Z-.' ]: will match English alphabets
For PHP use this : [^\\u0000-\\u007F]|[a-zA-Z-.']
For everyone that is looking for the solve, i found this temporary solution:
$regex = "/([\w-.' \á-\ÿ])+/";
Without dot and stuff but with spaces:
$regex = "/([a-zA-Z \á-\ÿ])+/";
Cheers
I have:
$input = str_replace('/all these symbols/', "", $input);
Can't really understand the pattern syntax for preg_match and preg_replace. Sometimes people use '+ - * ^ $ \s' and different kind of brackets there. Tried to read the manual, but don't really get it. Can i find somewhere more clear information about all the possibilities of the preg syntax? Thanks in advance.
Just put those characters into character class as shown in below example:
$replaced = preg_replace("/[“”!?;\",.\/”“']/u", " ", "hello!?; “how are you”");
print_r($replaced);
The output:
hello how are you
You can use squared brackets, just have to escape the right chars:
echo preg_replace('/[“”!?;",.\/”“\']/', '', 'a!b?c');
I am after (if possible), a reg expression which will only replace questions marks "?" with "'" where it is not followed by the "=" symbol in my string?
e.g. this is something?, but this will remain?=forever
should end up as:
this is something', but this will remain?=forever
Thanks
Mu
This is simple using a negative lookahead: Use
\?(?!=)
Just check this.. Simple one without regex ( I don't know regex :( )...:p
$string = "this is something?, but this will remain?=forever";
$my_secret_replace = "THISISANYTHINGWHICHWILLNOTINSTRING_OR_ANYRANDOMNUMBER";
$temp_string = str_replace("?=",$my_secret_replace,$string);
$temp_string = str_replace("?","'",$temp_string);
$final_string = str_replace($my_secret_replace,"?=",$temp_string);
$string = "String contains ? and ?= contains too?=?";
echo preg_replace("/\?([^=]|$)/", "'\\1", $string);
I have this combination in a string:
"I am tagging #username1.blah. and #username2.test. and #username3."
I tried this:
preg_replace('/\#^|(\.+)/', '', 'I am tagging #username1.blah. and #username2.test. and #username3. in my status.');
But the result is:
"I am tagging #username1blah and #username2test and #username3 in my status"
The above result is not what I wanted.
This is what I want to achieve:
"I am tagging #username.blah and #username2.test and #username3 in my status."
Could someone help me what I have done wrong in the pattern?
Many thanks,
Jon
I don't like regex very much, but when you are sure that the dots you want to remove are always followed by a space, you could do something like this:
php > $a = "I am tagging #username1.blah. and #username2.test. and #username3.";
php > echo str_replace(". ", " ", $a." ");
I am tagging #username1.blah and #username2.test and #username3
Try this:
preg_replace('/\.(\s+|$)/', '\1', $r);
This will replace dots at the end of "words" that are starting with #
$input = "I am tagging #username1.blah. and #username2.test. and #username3. in my status.";
echo preg_replace('/(#\S+)\.(?=\s|$)/', '$1', $input);
(#\S+)\.(?=\s|$) will match a dot at the end of a non whitespace (\S) series when the dot is followed by whitespace or the end of the string ((?=\s|$))
preg_replace('/\.( |$)/', '\1', $string);
How about:
preg_replace("/(#\S+)\.(?:\s|$)/", "$1", $string);
/\#\w+(\.\w+)?(?<dot>\.)/
That will match all dots and name them in the dot group
I have a text field in which user can enter any character he/she wants. But in server i have a string patter [a-z0-9][a-z0-9+.-]*, if any of the character in the value from the text box doesn't match the pattern, then i must remove that character from that string. How can i do that in php. is there any functions for that?
Thanks in advance.
Gowri Sankar
.. in PHP we use regular Expressions with preg_replace.
Here you have some help with examples...
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
this is what you need:
$new_text = preg_replace('#[^A-Za-z+.-0-9]#s','',$text);
Just use preg_replace with the allowed pattern negated.
For example, if you allow a to Z and spaces, you simply negate it by adding a ^ to the character class:
echo preg_replace('/[^a-z ]*/i', '', 'This is a String !!!');
The above would output: This is a String (without the exclamation marks).
So it's removing any character that is not a to Z or space, e.g. your pattern negated.
How about:
$string = 'A quick test &*(^&for you this should work';
$searchForThis = '/[^A-Za-z \s]/';
$replaceWithBlank = '';
echo preg_replace($searchForThis, $replaceWithBlank , $string);
Try this:
$strs = array('+abc123','+.+abc+123','abc&+123','#(&)');
foreach($strs as $str) {
$str = preg_replace('/(^[^a-z0-9]*)|([^a-z0-9+.-]*)/', '', $str);
echo "'",$str,"'\n";
}
Output:
'abc123'
'abc+123'
'abc+123'
''
str_replace('x','',$text);