Hi I have problem with correct url validation with query string containing email address like:
https://example.com/?email=john+test1#example.com
this email is ofc correct one john+test1#example.com is an alias of john#example.com
I have regex like this:
$page = trim(preg_replace('/[\\0\\s+]/', '', $page));
but it don't work as I expected because it replaces + to empty string what is wrong. It should keep this + as alias of email address and should cut out special characters while maintaining the correctness of the address.
Example of wrong url with +:
https://examp+le.com/?email=example#exam+ple.com
Other urls without email in query string should be validating correctly using this regex
Any idea how to solve it?
I think this is what you looking for:
<?php
function replace_plus_sign($string){
return
preg_replace(
'/#/',
'+',
preg_replace(
'/\++/i',
'',
preg_replace_callback(
'/(email([\d]+)?=)([^#]+)/i',
function($matches){
return $matches[1] . preg_replace('/\+(?!$)/i', '#', $matches[3]);
},
$string
)
)
);
}
$page = 'https://exam+ple.com/email=john+test1+#example.com&email2=john+test2#exam+ple.com';
echo replace_plus_sign($page);
Gives the following output:
https://example.com/email=john+test1#example.com&email2=john+test2#example.com
At first, I replaced the valid + sign on email addresses with a #, then removing all the remainings +, after that, I replaced the # with +.
This solution won't work if there's a #s on the URL if so you will need to use another character instead of # for the temporary replacement.
Related
I've been able to retrieve the Username from an instagram profile URL with regex, however it stops once it reaches a full-stop
.
Full URL:
https://www.instagram.com/username.test.uk/
The output from my regex:
https://www.instagram.com/username
My regex and output:
// Regex for verifying an instagram URL
$regex = '/(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_]+)/im';
// Verify valid Instagram URL
if ( preg_match( $regex, $instagram_url, $matches ) ) {
$instagram_username = $matches[1];
var_dump($instagram_username);
The var dump produces: username
Any obvious changes needed to my regex to exclude . in the username section?
So it stops because your match for the username doesn't include the ..
/(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_]+)/im
This should probably include \. within ([A-Za-z0-9-_]+). You also should escape the . elsewhere in your regex so it matches only the . character instead of anything.
/(?:(?:http|https):\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am)\/([A-Za-z0-9-_\.]+)/im
This will capture all alpha-numerics, underscores and dots.
For link that is generated by app 'nametag option'
https?:\/\/www\.?instagram\.com\/([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?\S)\?r=nametag
I am writing a code to read bounced emails from inbox. I am getting the body of the email like so:
$body = imap_body($conn, $i);
After I get the body string, I split it into an array with explode.
$bodyParts = explode(PHP_EOL, $body);
The bounced emails that I am concerned with, they all have a particular header set i.e. X-OBJ-ID. I can loop through $bodyParts to check if that particular header is set or not, but how do I get it's value if the header exists. Currently, the header string looks like this for those bounced emails which had that header set:
"X-OBJ-ID: 24\r"
So, basically my question is: How do I extract 24 from the above string?
Lookbehinds can be helpful in such cases
/(?<=X-OBJ-ID: )\d+/
(?<=X-OBJ-ID: ) look behind. Ensures that the digits is preceded by X-OBJ-ID:
\d+ Matches digits.
Regex Demo
Example
preg_match("/(?<=X-OBJ-ID: )\d+/", "X-OBJ-ID: 24\r", $matches);
print_r($matches)
=> Array (
[0] => 24
)
Try
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
or you can do it via regular expression
preg_replace("/[^0-9]/","",$string);
You could do something like so:
$str = "X-OBJ-ID: 24\r";
preg_match('X-OBJ-ID:\s+(\d+)', $str, $re);
print($re);
This should match your string and store the 24 within a capture group which will be then made accessible through $re.
try this code
preg_replace('/\D/', '', $str)
it removes all the non numeric characters from the string
My solution:
<?php
$string = '"X-OBJ-ID: 24\r"';
preg_match_all('^\X-OBJ-ID: (.*?)[$\\\r]+^', $string, $matches);
echo !empty($matches[1]) ? trim($matches[1][0]) : 'No matches found';
?>
See it working here http://viper-7.com/kuMyVh
Hi i am having an issue in trying to extract variables using preg replace .I guess i am messing with the delimiters or just doing it wrong
Subject
'file': 'EoWviKqVizQ,end=1384596943/data=B262F941/speed=375k/2305873_hd.flv',
I need to extract
end=1384596943/data=B262F941/speed=375k/1234_hd.flv
This is basically the string after the comma in between the single quotes.
My attempts
preg_match('#'file':'(.*)'#',$input , $matches)
preg_match("#'file':'(.*)'#",$input , $matches)
Hope someone can help me out
Regards
Just do this:
$input = "'file': 'EoWviKqVizQ,end=1384596943/data=B262F941/speed=375k/2305873_hd.flv',";
$mypart = preg_replace("/^'file': '[^,]+,/", "", $input); // strip first part, i.e., "'file': 'EoWviKqVizQ,"
$mypart = preg_replace("/',\s*$/", "", $mypart); // strip last part, i.e., "',"
echo $mypart;
EDITED based on OP feedback (replaced initial [^']+ with file to match only lines starting with 'file', etc.
I have an email address that could either be
$email = "x#example.com"; or $email="Johnny <x#example.com>"
I want to get
$handle = "x"; for either version of the $email.
How can this be done in PHP (assuming regex). I'm not so good at regex.
Thanks in advance
Use the regex <?([^<]+?)# then get the result from $matches[1].
Here's what it does:
<? matches an optional <.
[^<]+? does a non-greedy match of one or more characters that are not ^ or <.
# matches the # in the email address.
A non-greedy match makes the resulting match the shortest necessary for the regex to match. This prevents running past the #.
Rubular: http://www.rubular.com/r/bntNa8YVZt
Here is a complete PHP solution based on marcog's answer
function extract_email($email_string) {
preg_match("/<?([^<]+?)#([^>]+?)>?$/", $email_string, $matches);
return $matches[1] . "#" . $matches[2];
}
echo extract_email("ice.cream.bob#gmail.com"); // outputs ice.cream.bob#gmail.com
echo extract_email("Ice Cream Bob <ice.cream.bob#gmail.com>"); // outputs ice.cream.bob#gmail.com
Just search the string using this basic email-finding regex: \b[A-Z0-9._%+-]+#[A-Z0-9.-]+.[A-Z]{2,4}\b
It will match any email in any text, and in your first string it will match the whole string, and in the second, only the part of the string that is e-mail.
To quickly learn regexp this is the best place: http://www.regular-expressions.info
$email = 'x#gmail.com';
preg_match('/([a-zA-Z0-9\-\._\+]+#[a-z0-9A-Z\-\._]+\.[a-zA-Z]+)/', $email, $regex);
$handle = array_shift(explode('#', $regex[1]));
Try that (Not tested)
I have such text:
<Neednt#email.com> If you do so, please include this problem report.
<Anotherneednt#email.com> You can delete your
own
text from the attached returned message.
The mail system
<Some#Mail.net>: connect to *.net[82.*.86.*]: Connection timed
out
I have to parse email from it. Could you help me with this job?
upd
There could be another email addresses in <%here%>. There should be connection between 'The mail system' text. I need in email which goes after that text.
Considering this text is stored in $text, what about this :
$matches = array();
if (preg_match('/<([^>]+)>/', $text, $matches)) {
var_dump($matches[1]);
}
Which gives me :
string 'Some#Mail.net' (length=13)
Basically, I used a pretty simple regex, that matches :
a < character
anything that's not a > character : [^>]
at least one time : [^>]+
capturing it : ([^>]+)
a > character
So, it captures anything that's between < and >.
Edit after comments+edit of the OP :
If you only want the e-mail address that's after The mail system, you could use this :
$matches = array();
if (preg_match('/The mail system\s*<([^>]+)>/', $text, $matches)) {
var_dump($matches[1]);
}
In addition to what I posted before, this expects :
The string The mail system
Any number of white-characters : \s*
You want to use preg_match() and looking at this input it should be simple:
<?php
if (preg_match('/<([^>]*?#[^>]*>/', $data, $matches)) {
var_dump($matches); // specifically look at $matches[1]
}
There are other patterns that would match it, you don't have to stick to that same pattern. The '<' and '>' in your input are helpful here.