Grab Number using Preg_Match - php

$str = 'title="room 5 stars"';
preg_match_all('/title="([0-9]+)"/sm', $str, $rate);
I need to grab number 5 from title. The regex doesn't work!
If i do this:
preg_match_all('/title="([0-9]+)"/sm', $str, $rate);
I get:
room 5 stars
However, this one doesn't return anything:
'/title="([0-9]+)"/sm'
Where did i go wrong?

You're not taking into account the words around the number, try this:
$str = 'title="room 5 stars"';
preg_match_all('/title=".*(\d+).*"/', $str, $rate);
// The number is then in $rate[1][0];

You forgot to match the text before and after your number.
Try with : /title=".*([0-9]+).*"/
PS: you don't need m and s option

* is a greedy match, it might give wrong results sometimes.
You can use /title=".*?(\d+).*?"/ which is a lazy match and will search the least characters.
You can also try this free tool for regex matching: RegExr

Related

PHP extract only 4digit numbers from string containing 4digit,5digit,6digit numbers

enter image description herei have tried many php functions like strpos(), preg_match() but none of them works. i have a string
i want to extract only the four digit number which is 1234.
<?php
$texxt="abcd1245 784563 1234 98756 kfg7456178";
$results=array();
preg_match('/[0-9]{4}/', $texxt, $results);
print_r($results);
?>
but the above code return 1245 instead of 1234.if i remove the abcd1245 then the out put is 7845.the actual string is very large it containg more than 200 numbers like above. i want only the exact 4 digit number. is there any way to solve this?
You need to place boundaries on both sides of your pattern.
\b\d{4}\b
An alternative would be to use \s instead of \b for whitespace - because boundaries will match other non-alphanumeric characters. Depends on exactly what you're looking for.
See it here
As you said you have more than 200 numbers then use below code:
<?php
$texxt="abcd1245 784563 1234 3421 98756 kfg7456178";
$results=array();
preg_match_all('/\b\d{4}\b/', $texxt, $results);
print_r($results);
?>
preg_match check for only one occurrence, where as preg_match_all check all occurrences.
For regex explanation please refer doc.

PHP Regex to get text between 2 words with numbers

i'm trying to get the string between two words in a entire string:
Ex.:
My string:
...'Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado'...
I'm using
/(?<=Total a Facturar )(.*?) Recepcionado/
I need the highlighted characters (26,860161,16080,580310,760)
and i get 221,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado with my pattern.
The numbers of the string are always different, i need the numbers that are together without a space.
Thanks
EDIT:
Here is the entire string: eval.in/802292
I hope this will be helpful
Regex demo or Regex demo 2
Regex: (?:\d+(?:\,\d+){2,})
For above question you can also use it like this (?:\d+(?:\,\d+){4})
1. (?:\d+) this will match digits one or more.
2. (?:\,\d+){2,} Adding this in expression will match patterns like , and digits {2,} for 2 or more than 2 times.
PHP code: Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Total a Facturar 123,061 221,063 26,860161,16080,580310,760 358,297 Recepcionado";
preg_match("#(?:\d+(?:\,\d+){2,})#", $string, $matches);
print_r($matches);

Regex to find digits between the last set of parentheses

i was struggling to make a regex which extract the digits bettween the last set of parentheses from a string, and this is what i came with until now:
^.*?\([^\d]*(\d+)[^\d]*\).*$
E.g.:
This is, (123456) a string (78910);
It returns 123456 , which is great but i need it to look at the last set and return 78910.Also , i want the regex to ignore everything but digits:
This is, (123bleah456) a string (789da10);
Should return: 78910
UPDATE
Using regex:
(\d+)(?!.*\d)
For string:
Telefon Mobil (123)Apple iPhone 6 128GB Gold(1567)asd234
Will return 234 when it should be 1567
Rubular
You can extract the last one by use of greed:
.*\(\K\d+
See demo at regex101
\K resets beginning of the reported match.
For your more specific updated case slightly modifiy the regex and strip out non-digits.
$str = "This is, (123bleah456) a string (789da10);";
if(preg_match('/.*\(\K\d[^)]*/', $str, $out))
$res = preg_replace('/\D+/', "", $out[0]);
See demo at eval.in

Grabbing number next to a dollar sign with optional thousands and decimals

I am trying to grab a number that can be in the format $5,000.23 as well as say, $22.43 or $3,000
Here's my regular expression, this is in PHP.
preg_match('/\$([0-9]+)([\.,]*)?([0-9]*)?([\.])?([0-9]*)?/', $blah, $blah2);
It seems to match numbers in the format $5,500.23 perfectly fine, however it doesn't seem to match any other numbers well, like $0.
How do I make everything optional? Shouldn't grouping () and using a question mark do that?
This should do the trick:
\$[\d,.]*[\d]
Debuggex Demo
Specific PHP Example:
$re = "/\\$[\\d,.]*[\\d]/";
$str = "\$1 klsjdfgsjdfg \$100 kjdfhglsjdfg \$1,000 jljsdfg \$1,000.00 ldfjhsdf";
preg_match_all($re, $str, $matches);
Regex 101 Demo

Position in string of 4 digits number begining with "20"

I've got a string:
$string = "Something here 2014 another text here";
I need to detect position of the first 4 digits number that begins with "20".
So the result of the example would be 15th character of the $string.
Since you have commented with code you tried, I now feel comfortable answering your question properly :) Thank you for trying first!
Your attempt:
preg_match('/20\d\d/', "Something here 2014 another text here",
$matches, PREG_OFFSET_CAPTURE);
... is absolutely correct, however as you correctly pointed out, it would also match 20140 (and indeed 12014 would match too).
To fix this behaviour, you can add word boundaries - because numbers count as word characters. Your regex becomes:
'/\b20\d\d\b/'
This will ensure that there are no numbers (or letters, for that matter) immediately before or after your target four-digit number :)
What about...
$needle = "20";
$pos = strpos($string , $needle);
EDIT:
as requested, a way to get the string from this
$date = substr ($string , $pos , 4 ]);

Categories