i use preg replace since my column database does not support "strange letters"
but after regex i need keep "/", in this code bellow "/" is always missing
in code bellow i need to get all letter complete
<?php
$jurnalName = "TL 110/90-12 K93-N02 AHM+";
$name = htmlspecialchars(htmlentities($jurnalName));
$name = preg_replace('/[^A-Za-z0-9|\- +]/', '', $name);
var_dump($name);
the result is always "TL 11090-12 K93-N02 AHM+" what i expecting is complete "TL 110/90-12 K93-N02 AHM+"
Add / to the list of chars you want to keep
<?php
$jurnalName = "TL 110/90-12 K93-N02 AHM+";
$name = htmlspecialchars(htmlentities($jurnalName));
$name = preg_replace('/[^A-Za-z0-9|\-\s\+\/]/', '', $name);
var_dump($name);
(I also changed the space for \s and scaped the plus +, its optional here but I think its a good practice for characters that have spetial meaning inside regex
Related
I'm trying to evaluate some strings containing dashes with the symfony ExpressionLanguage component.
Here is what I've got so far :
...
$string = 'user.chuck-norris.getId()';
$language = new ExpressionLanguage();
$evaluated = $language->evaluate($expression, $users);
...
This returns me the following error :
Variable "norris" is not valid around position 12. (Symfony\Component\ExpressionLanguage\SyntaxError)
If I change the dash "-" by an underscore "_", this works, but I have slug system which use dash and I dont wont to change it if I can avoid it.
Is there any solution?
Thanks
Like stated by Yonel, dashes are interpretated as operator.
So for this to work, I just have to replace dashes by undescores
$string = 'user.chuck-norris.getId()';
And then before making the request, replace _ by -
$value = str_replace('_', '-', $value);
On my website, after a user registers they can change their username at any time. The minimum amount of characters is 6 and max amount is 25.
Here's some of the coding to check the length and remove characters:
$users_new_name = strip_tags($_POST['new_name']);
$new_name = preg_replace("/[^0-9a-zA-Z -]/", "", $users_new_name);
// Check Length
if (ctype_space($new_name)) {
$message = die('<span class="Fail">Error: Name Must Be At least 6 Characters Long!</i></span>');
}
if(strlen($new_name) < 6) {
$message = die('<span class="Fail">Error: Name Must Be At least 6 Characters Long!</i></span>');
}
if(strlen($new_name) > 25) {
$message = die('<span class="Fail">Error: Name Can\'t Be More Than 25 Characters Long!</i></span>');
}
The issue I'm having is if you type in 5 spaces and then a letter or number, There new name will be that letter or number; Or if you type in a letter or number then 5 spaces. How could I prevent this from happening?
Here's a screenshot of the example
I do not understand why tags would be in a POST.
Spaces becomes a non-issue if you change:
$users_new_name = strip_tags($_POST['new_name']);
To
$users_new_name = trim(strip_tags($_POST['new_name']));
Or ideally (strip tags is unnecessary):
$users_new_name = trim($_POST['new_name']);
Change the RegEx expression to /[^0-9a-zA-Z]/ to eliminate spaces and dashes.
It sounds like you need trim() to trim any spaces from the username. See http://php.net/trim
ltrim() will trim any leading spaces. rtrim() will trim any spaces at the end of the string.
This is fairly simple and it's something I've had to deal with on systems before.
If you make the first thing you call, this preg_replace, the rest of you code should catch it;
$name = preg_replace('~[\s]+~', '', $name);
This simply checks if there is a space, or more than one, then replaces with a single space.
So " l" would return " l" - failing your 5 character minimum.
Use trim() around this and you should have it working to something acceptable (Depending on your definition of acceptable - worked in my use-case)
I set formatting mask on a textfield :
$(document).ready(function() {
$("#budget_para_prevision").inputmask("9999999999999.99"); // 13 digits before "."
});
The problem happens after posting the form when the length of the digits before the "." sign is less than 13 then the formatting characters are written automatically with the $_POST variable , it gives something like this :
391000000000_.__
So how to remove the _ and the . sign in this case ?
You can remove the unwanted characters using a combination of str_replace and rtrim. Something like this:
$input = "391000000000_.__";
$result = str_replace("_", "", $input); // Remove instances of underscore.
$result = rtrim($result, "."); // Remove the dot if it's the last character.
Or you can just do the whole lot with a single rtrim:
$result = rtrim($input, "._");
You can have part of your mask be optional. Anything listed after '?'
within the mask is considered optional user input. The common example
for this is phone number + optional extension.
$(document).ready(function() {
//if 12 and more digits are optional
$("#budget_para_prevision").inputmask("999999999999?9.99");
});
From docs
I guess it would depend on what jQuery inputmask plugin you are using exactly, but if it is Robin Herbots plugin, you can make parts of your input optional and specify lengths:
$(document).ready(function() {
$("#budget_para_prevision").inputmask("9{1,13}[.99]"); // 13 digits before "."
});
Of course you could also fix it at the backend with rtrim($input, "._") but preventing the input in the first place would be better.
im using this for my Validation:
$space = "[:blank:]";
$number = "0-9";
$letter = "\p{L}";
$specialchar = "-_.:!='\/&*%+,()";
...
$default = "/^[".$space.$number.$letter.$specialchar."]*$/";
if (!preg_match($all, $input)){
$error = true;
}
The Problem i have is:
all is working except "ü"... "Ü" is but "ü" not and i dont know why?
\p{L} should accept all letters and special letters... i dont get it why its not working :(
anyone an idea what i can do?
The data i try to validate is a POST Value from a registration FORM
// p.s. if im using \p{L}ü i get an error like this:
Compilation failed: range out of order in character class at offset 23 in...
Escape the dash:
$specialchar = "\-_.:!='\/&*%+,()";
# here __^
Also add the /u modifier for unicode matching:
$default = "/^[".$space.$number.$letter.$specialchar_def."]*$/u";
# here __^
Test:
$space = "[:blank:]";
$number = "0-9";
$letter = "\p{L}";
$specialchar = "\-_.:!='\/&*%+,()";
$default = "/^[".$space.$number.$letter.$specialchar."]*$/u";
// wrong variable name ^^^^^^^^^^^^ in your example.
$input = 'üÜ is but ';
if (!preg_match($default, $input)){
echo "KO\n";
} else {
echo "OK\n";
}
Output:
OK
The problem is the position that the hyphen is placed in. Within a character class you can place a hyphen as the first or last character in the range. If you place the hyphen anywhere else you need to escape it in order to add it to your class.
$specialchar = "_.:!='\/&*%+,()-";
Also you need to add the u (Unicode) modifier to your regular expression. This modifier turns on additional functionality of PCRE and pattern strings are treated as (UTF-8) and you have the wrong variable in the pattern.
$default = "/^[".$space.$number.$letter.$specialchar."]*$/u";
The - is a special character in character class used to specify a range of character. When you construct the regex by string concatenation, it is recommended that you escape it \-.
Including the fix above, there is another problem in "\-_.:!='\/&*%+,()". Do you want to include \ and /? Or only one of them?
If you want to include both, you should specify it as "\-_.:!='\\\\\/&*%+,()".
If you don't want to escape /, you can replace your separator / in the construction of $default to something not used in your regex, for example ~. In that case, the list of special character will have one less \: "\-_.:!='\\\\/&*%+,()".
I'm trying to replace the domain name of email addresses, between # and . with ***
For example:
$email1 = info#mytestdomain.com
$email2 = info#mytestdomain.net
Need to be become
$email1 = info#***.com
$email2 = info#***.net
I know I can use the PHP preg_replace function but I'm not sure what regex I need to use in my case. So my question is, which regex should I use in my case to replace everything between # and . with ***?
Thanks
You can use this assertion based regex.
$eml = preg_replace('/#\K[^.]+/', '***', $eml);
Live Demo
Live demo
$email1 = "info#mytestdomain.com";
echo preg_replace("/(.*#)([^\.]+)(\..*)/","$1***$3",$email1);
Output:
info#***.com
You could use a positive lookahead also.
$email1 = "info#mytestdomain.com";
echo preg_replace("/[^#]+(?=\.)/","***",$email1);
Pattern Explanation:
[^#]+(?=\.) Matches any character but not of # one or more times only if the characters are followed by a literal dot.