Regular expressions match first number next to a given character - php

how can I match a the first number next to a % character ?
<?php
$string = 'Get 30% off when you spend over £100 on electronics';
if(strpos($string,'% off') !== false) {
$number = preg_replace("/[^0-9%]/", '', $string);
return $number;
}
this returns 30%100
any help would be great thanks in advance.

This regex will match (and capture) all digits preceding a '%' sign:
'/(\d+)%/'
You can try something like this:
$string = 'Get 30% off when you spend over £100 on electronics';
preg_match('/(\d+)%/', $string, $matches);
print_r($matches[1]);
Do let us know if your requirements are more complex.

Regex:
\d{1,3}%
Explained:
\d{1,3} match a digit [0-9]
Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]
% matches the character % literally

This seems to do the trick :)
if(strpos($string,'%') !== false) {
$regex_percent = "/((\d{1,5})(?:%))/";
preg_match($regex_percent, $string, $matches_off);
$number = $matches_off[2];
return $number;
}

Related

How to use regular expression to find all numbers starting with "$"

I'm dealing with strings that contain non-comma-separated dollar values. For example:
"LOT 2 - $650000"
I need to be able to find the "$650000" and replace it with "$650,000".
The problem is, I'm a complete novice when it comes to regular expressions. I found a post that suggested this for finding numbers:
preg_match_all('!\d+!', $string, $matches);
This does successfully find both the "2" and the "650000" in my string. However, I want to make sure I only get numbers that start with "$", so I only want to get the "$650000".
Can anyone help me adapt the regular expression to get only numbers that start with "$"?
Kevin's answer is better. I went the long way around:
<?php
$dollarString = 'I would like $100000000000 more than I would like $10000000 but that is still better than $1000 and $99 problems.';
echo '<p>dollarString: ';
var_dump($dollarString);
echo '</p>';
function addCommas ($matches){
$output = [];
$number = $matches[1];
$j = 1;
for($i=strlen($number)-1; $i>=0; $i--){
array_push($output, $number[$i]);
if($j%3 == 0 && $i != 0 && $i != strlen($number)-1){array_push($output, ',');}
$j++;
}
array_push($output, '$');
$output = array_reverse($output);
return implode($output);
}
$newString = preg_replace_callback('#\$(\d+)#', 'addCommas', $dollarString);
echo '<p>newString: ';
var_dump($newString);
echo '</p>';
?>
Just add the dollar sign in your pattern and use preg_replace_callback then combine number_format. Something like this:
$string = preg_replace_callback('~\$(\d+)~', function($matches) {
return !empty($matches[1]) ? '$' . number_format($matches[1]) : null;
}, $string);
You could replace matches of the following regular expression with a comma to both confirm the presence of the dollar sign and to insert commas in the correct locations.
/(?:\$|\G)\d+?\K(?=(?:\d{3})+$)/
Start your engine!
The PCRE engine performs the following operations.
(?: : begin non-capture group
\$ : match '$'
| : or
\G : assert position at the end of the previous match
) : end non-capture group
\d+? : match 1+ digits
\K : reset starting point of match and no longer include
previously-consumed characters in reported match
(?= : begin positive lookahead
(?:\d{3}) : match 3 digits in a non-capture group
+ : execute non-capture group 1+ times
$ : match end of string
) : end positive lookahead
Try the following:
preg_match_all('!\$\d+!', $string, $matches);
This website really helped me understand how to achieve this

preg replace everything BUT digits over 5 in length

I have a string:
3 pk. Ready-Dough White Loaves Included $3.99 - 47500 - 00892, 48101
I want to keep only groups of digits longer than 5 characters, and if possible, any dashes or commas between them.
e.g.
47500-00892,48101
My first step was to strip out groups of digits < 4:
preg_replace('/\d{1,4}/', '', $string);
My thinking was "replace any block of digits from 1 to 4 with nothing", but that doesn't do exactly what I thought. Maybe I'm just missing an operator?
Then I was going to strip out all letters and all punctuation except , and -. In my example I would've been left with a starting - because of it being in a string, but a trim() would've been fine to clean that up.
Any help is appreciated!
Had I been patient for 5 more minutes, I would've found the answer: \b
For some reason, working with digits didn't trigger that I needed to use 'word boundaries'.
$string = preg_replace('/\b\d{1,4}\b/', '', $string);
$string = preg_replace('/[^0-9-,]/', '', $string);
$string = trim($string, ',-');
Since there's no reason to perform a replacement, you can use preg_match_all to take what you want and reduce the result array:
$re = '/\d{5,}(?:(?=\s*([-,])\s*\d{5}))?/';
$str = '3 pk. Ready-Dough White Loaves Included $3.99 - 47500 - 00892, 48101';
if ( preg_match_all($re, $str, $matches, PREG_SET_ORDER) ) {
$result = array_reduce($matches, function ($c,$i) { return $c . implode('', $i); });
echo $result;
}

How do i parse a Google Voice email address with PHP/Regex?

I would like to extract 16197226146 from the following string using PHP:
"(480) 710-6186" <18583894531.16197226146.S7KH51hwhM#txt.voice.google.com>
Could someone please help me with the regex please?
<\d*?\.(\d+)
< Match "<"
\d Match digits
* 0 or more times
? Lazy, take as little as possible
\. Match a "."
( Capture
\d Match digits
+ 1 or more times
) Stop capturing
That matches the second number after a .. The match is in group 1.
if (preg_match("/<\d*?\.(\d+)/", $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
You can play with the regex here.
You could use explode.
$value = "(480) 710-6186"<18583894531.16197226146.S7KH51hwhM#txt.voice.google.com>";
$result = explode('.', $value);
echo $result[1]; // is 16197226146

Regex - number from string which is not 1

Need a regex to get numbers of pages in the thread:
example url : traidnt.net/vb/f25
i tried this :
'~<td class="vbmenu_control" style="font-weight:normal">.*([2-9]{1}|[0-9]{2,}).*</td>~isU'
but it wont work.
Thanking you
/.*?([0-9]{2,}|[2-9]{1}).*/s
matches a one-digit number > 1 or any multi-digit number.
please note that this does not match correctly when: "page 1 of 1 pages"
if the string is fixed, you better go with:
/page \d+ of (\d+) pages/is
or if the string is not absolutely fixed but you want the second number from the string you may use:
/\D*(\d+)\D*(\d+)\D*/s
and use the second sub-match. (will also match correctly when "page 1 of 1 pages".
You can use \d+ to match numbers, and a negative assertion (?!...) to exclude something. Often you need some anchors around to make it work reliably, here word boundaries:
/\b(?!1\b)\d+\b/
This will find all digits, which are not one:
/[^\D1]/
<?php
$str="page 1 of 200 pages";
$matches = array();
if (preg_match('/page (\d+) of (\d+)/', $str, $matches)) {
echo $matches[2];
}
?>
for the updated question:
$url="traidnt.net/vb/f25";
$matches1 = array();
if (preg_match('/f(\d+)/', $url, $matches1)) {
echo $matches1[1];
}
?>

PHP Check For One Letter and One Digit

How can I check if a string is at least one letter and one digit in PHP? Can have special characters, but basically needs to have one letter and one digit.
Examples:
$string = 'abcd' //Return false
$string = 'a3bq' //Return true
$string = 'abc#' //Return false
$string = 'a4#e' //Return true
Thanks.
Try this
if (preg_match('/[A-Za-z]/', $string) & preg_match('/\d/', $string) == 1) {
// string contains at least one letter and one number
}
preg_match('/\pL/', $string) && preg_match('/\p{Nd}/', $string)
or
preg_match('/\pL.*\p{Nd}|\p{Nd}.*\pL/', $string)
or
preg_match('/^(?=.*\pL)(?=.*\p{Nd})/', $string)
or
preg_match('/^(?=.*\pL).*\p{Nd}/', $string)
I'm not sure if \d is equivalent to [0-9] or if it matches decimal digits in PHP, so I didn't use it. Use whichever of \d, [0-9] and \p{Nd} that matches that right thing.
The pattern you're looking for is ^.*(?=.*\d)(?=.*[a-zA-Z]).*$
In use:
if( preg_match( "/.*(?=.*\d)(?=.*[a-zA-Z]).*/", $string ) )
echo( "Valid" );
else
echo( "Invalid." );
That should work if latin chars only and numbers:
if (preg_match('/[a-z0-9]+/i', $search_string)) { ... has at least one a-zA-Z0-9 ... }

Categories