I am trying to make an account generator with censured passwords, and I don't want to replace all characters with just 10 *'s. I want it to be like this:
if the password is 15 characters long, it will be replaced with 15 *'s. I tried to use this:
$censpass = preg_replace('/[a-zA-Z0-9\']/', '*', $accounts[$i]['password']);
but as you might know, that doesn't work for !'s. How can I use preg_replace with every single character in PHP?
If someone doesn't understand:
I want this: "password123!"
to be replaced with this: "************" with the accurate length using preg_replace
If this exists somewhere else, please link it below, I tried to find this but I could only find how to replace some characters, like numbers only
Thank you :)
For your goal I'd use a different approach, such as:
$encpass = str_pad('', strlen($accounts[$i]['password']), '*');
In fact, there is no need to use a regular expression (which is slow and resource consuming) just to generate a string the same length as another one.
Anyway, if you still want to use your solution, the correct regexp for your use case is simply a . such as:
$censpass = preg_replace('/./', '*', $accounts[$i]['password']);
Have a look here: http://php.net/manual/en/regexp.reference.dot.php
Related
I think I need to use the preg_replace function but not sure exactly how to type in the patterns I want to find and replace. Basically, I want to replace this:
: u"x"x",
with this:
: u"x'x",
x means that any characters can go there. But I don't know how to write the x in PHP.
Thank you!
Edit: basically, I want to replace that middle double-quote with a single-quote. And I'll be searching through a big JSON file to do it. Probably should have said this at the start.
You could use this regular expression:
$result = preg_replace('#(: u".*?)"(.*?")#', "$1'$2", $string);
I have strings like this:
$str1="YESYES|c|no|c|";
$str2="YESYES|c|not this|c|YES|c|or this|c|";
$str3="YES";
I want strings like this:
$str1="YESYES";
$str2="YESYESYES";
$str3="YES";
I thought I could use preg_replace but the fact that I'm looking at a pipe seems to cause trouble, if I go like this:
$i=preg_replace("//|c\|[\s\S]+?/|c\|/",'',$i);
I get an 'unknown modifier "|"' error. I know this must have been asked before but it's very hard to search for. What is the proper regex to use?
You have to use the next line:
$i=preg_replace("/\|c\|[\s\S]+?\|c\|/",'',$i);
which is almost identical to what you have, but using \ instead of / to scape the pipes
So in my string, I have certain sections with hashes. For example, consider the string "#Hello, this is a sample string. This is another example of ###hashes".
I want to replace that with:
"##Hello, this is a sample string, This is another example of ####hashes".
(note that the number of hashes in each instance increased by one)
However, I'm not too sure how. I'd imagine it involved regular expressions, and I've searched a bit, but I'm not too sure what to do.
Can anyone help/lead me on the right path?
Cheers
preg_replace('/(#[^#])/', '#\1', $string);
This works too:
preg_replace('/#+/', '#$0', $string);
so I am trying to match word in a wall of text and return few words before and after the match. Everything is working, but I would like to ask if there is any way to modify it so it will look for similar words. Hmm, let me show you an example:
preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*(pripravená)(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]);
This code returns a match, but I would like it to modify it so
preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*(pripravena)(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]);
would also return a match. Its slovak language and I tried with range of unicode characters and also with \p{Sk} (and few others) but to no avail. Maybe I just put it in the wrong place, I dont know...
Is something like this possible?
Any help is appreciated
I don't know if there is a "ignore accent" switch. But you could replace your search query with something like:
$query = 'pripravená';
$query = preg_replace(
array('=[áàâa]=i','=[óòôo]=i','=[úùûu]=i'),
array( '[áàâa]' , '[óòôo]' , '[úùûu]' ),
$query
);
preg_match_all('/(?:\b(\w+\s+)\{1,5})?.*('.$query.')(?:(\s+){1,2}\b.{1,10})?/u', $item, $res[$file]);
That would convert your 'pripravená' query into 'pripraven[áàâa]'.
You could use strtr() to strip out the accents: See the PHP manual page for a good example - http://php.net/manual/en/function.strtr.php
$addr = strtr($addr, "äåö", "aao");
You'd still need to specify all the relevant characters, but it would be easier than using a regex to do it.
(pripraven[áa]) or (pripravena\p{M}*) or, more likely, some combination of these approaches.
I don't know of any other, more concise, way of specifying "all Latin-1 vowels that are similar to 'a' in my current locale".
I have an input for users where they are supposed to enter their phone number. The problem is that some people write their phone number with hyphens and spaces in them. I want to put the input trough a filter to remove such things and store only digits in my database.
I figured that I could do some str_replace() for the whitespaces and special chars.
However I think that a better approach would be to pick out just the digits instead of removing everything else. I think that I have heard the term "whitelisting" about this.
Could you please point me in the direction of solving this in PHP?
Example: I want the input "0333 452-123-4" to result in "03334521234"
Thanks!
This is a non-trivial problem because there are lots of colloquialisms and regional differences. Please refer to What is the best way for converting phone numbers into international format (E.164) using Java? It's Java but the same rules apply.
I would say that unless you need something more fully-featured, keep it simple. Create a list of valid regular expressions and check the input against each until you find a match.
If you want it really simple, simply remove non-digits:
$phone = preg_replace('![^\d]+!', '', $phone);
By the way, just picking out the digits is, by definition, the same as removing everything else. If you mean something different you may want to rephrase that.
$number = filter_var(str_replace(array("+","-"), '', $number), FILTER_SANITIZE_NUMBER_INT);
Filter_Var removes everything but pluses and minuses, and str_replace gets rid of those.
or you could use preg_replace
$number = preg_replace('/[^0-9]/', '', $number);
You could do it two ways. Iterate through each index in the string, and run is_numeric() on it, or you could use a regular expression on the string.
On the client side I do recommand using some formating that you design when creating a form. This is good for zip or telephone fields. Take a look at this jquery plugin for a reference. It will much easy later on the server side.