validate Regex in php [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How this regex works?
'/[_a-z0-9-]+(\.[_a-z0-9-]+)*\s*#\s*[a-z0-9-]+(\s*\.\s*[a-z0-9-]+)*(\s*\.\s*[a-z]{2,6})/ie'
I got this regex on web, I use cheatsheet but could not understand it.

Considering the # symbol, that looks like validation for email addresses.
Explanation:
The white-space characters are set to +, between zero and unlimited. They are not necessary. What is necessary however is the # symbol and between 2 and 6 characters after a final . This is entirely indicative of an email address.
If I sent an email to some guy #whatever.com, perhaps a typo, I could simply strip the white-space before sending rather than spit out an error.
The addition of allowing white-spaces could also mean this is used for a list of email addresses. Note that with the global modifier, the following would match 4 email addresses with the regular expression:
apple#man.com,
thatguy#something.org,
holy#moly.com,
regex#friends.net

Related

Regex (.*) and random string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I changed the Permalink on WP to get any strings after the path. I use the regex: "yourdomain.com/%postname%-(.*)/"
When I am checking: "yourdomain.com/%postname%-f46eb54b99ce3a9835ea7d63e075d434", it matches.
But when I check:
"yourdomain.com/%postname%-446eb54b99ce3a9835ea7d63e075d434" then it returns "yourdomain.com/%postname%-(.*)/446/".
I think (. *) Will fit in everything, regardless of letters or numbers. I appreciate anyone who can explain it to me.
You should escape all / and ., if you mean them as normal symbols. So, you'll have:
yourdomain\.com\/%postname%-(.*)\/
It must not match
yourdomain.com/%postname%-f46eb54b99ce3a9835ea7d63e075d434
or
yourdomain.com/%postname%-446eb54b99ce3a9835ea7d63e075d434
, because you regex demands / at the end. If it is not obligatory, put ? after the ending \/.
yourdomain\.com\/%postname%-(.*)\/?
tests

PHP - preg_match explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Im a beginner in PHP I just want to ask can someone explain to me this line of code.
(preg_match('/^\w{5,}$/', $username))
Thankyou in advance. :) Your answer is so much appreciated. :)
Your PHP match string is
/^\w{5,}$/
and a PHP match string is surrounded by / characters which are not part of the RegEx string itself.
According to the comments your problem is about understanding regular expressions, not PHP.
^ is the beginning of the line, correct
$ is the end of the line, correct
\w Any word character (letter, number, underscore)
a{5,} does mean 5 or more characters 'a'
Therefore: If there are 5 or more any word characters in the username the function returns a positive result.
Or even easier: A username needs to contain at least five any word characters.
Learn more about regular expressions and how they work. Some explanation can be found in this comment.

Find Specific IP Address in a String with PHP preg_match() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My PHP code needs to decide whether or not a specific IP address is in the string $ipaddress:
preg_match("[1][4]\.[1][0][8]\.[2][4][1]\.[6][9]", $ipaddress )
This doesn't work for some reason. Any ideas why it doesn't work?
[1] and 1 is the same thing, there is no point in creating groups of one element. You can look straight for 14\.108\.241\.69.
What is missing for this to work are the delimiters. When using Perl regular expressions the pattern must be enclosed by delimiters, typically /, # or ~.
preg_match('/14\.108\.241\.69/', $ipaddress)
preg_match('~14\.108\.241\.69~', $ipaddress)
preg_match('#14\.108\.241\.69#', $ipaddress)
Are all valid uses of preg_match().
Because you are looking for a simple piece of string, you don't really need preg_match(), a simple strstr($ipaddress, '14.108.241.69') !== FALSE will do the job just as well and faster.

How can I strip undisplayable characters from a string with PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a feed that pulls from social feeds. These feeds can sometimes include special characters such as emojis that don't translate well:
I just want to strip out any characters that can't be displayed. I know that might remove a few that I could want but that's fine. I know I can do a regex but not really sure how to target like what's in the photo.
---EDIT---
This is what is throwing the characters above:
Try using PHP Sanitize Filters, most specifically FILTER_SANITIZE_SPECIAL_CHARS
<?php
$a = 'text including "unprintable" characters';
$sanitized_a = filter_var($a, FILTER_SANITIZE_SPECIAL_CHARS);
echo $sanitized_a;
?>
Just be careful using this if there are some characters you want (like < and > for instance)! Check the link at the top for a full list of filters you can choose from.

Detect partial email pattern in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing an unsubscribe option of email newsletters.
I have to detect emails of following format:
<0 or more alphanumeric letter/digit only>, then one <#> character, then <1 or more alphanumeric letter/digit>, then a <.> character, then <at least 2 alphanumeric letter/digits>
I need "zero" or more alphanumeric character before # character and not "one" or more because sometimes I want to unsubscribe whole domain names, so in that case the pattern to match is #example.com, and I also want to detect full email, it starts with an alphanumeric character.
How can I write the code to detect?
I take the email from url as $_GET['email']
For example url will be:
http://www.example.com/php/unsubscribe.php?email=#example.com
http://www.example.com/php/unsubscribe.php?email=#example.co
http://www.example.com/php/unsubscribe.php?email=abc#example.co
well the Regex then simply reads
/#.+\.[^\.]{2,}$/
EDIT: I used .* and [^\.] even though the OP asked for "alphanumeric letter/digit" - however, valid E-Mail addresses include stuff like dashes, underscores etc... Completely matching ALL valid email adresses is incredibly complex! (see: http://www.regular-expressions.info/email.html)
You could use the below regex.
[A-Za-z0-9]*#[A-Za-z0-9]+\.[A-Za-z0-9]{2,}
DEMO
[A-Za-z0-9]{2,} matches two or more alphanumeric characters. You could specify the character range inside {} paranthesis.

Categories