PHP Multiple digits regular expression - php

I need to extract the digits from the following string using regular expression:
pc 32444 xbox 43567
so my array will be
array ([0] => 32444 [1] => 43567)
Can someone help construct a preg_match for me?
Thanks!

Try this regular expression:
/\d+/
But you would need to use preg_match_all to get all matches:
preg_match_all('/\\d+/', $str, $matches)
$matches[0] will then contain the array you’re looking for.

Related

Regular expression can't match the whole Bengali word

I'm trying to use regular expression to match hashtags. When the language of a hashtag is English or Chinese, my code works fine. But when the language is Bengali, my code can't match the whole Bengali word.
Here is the code I'm testing with:
<?php
$hashtag = '#আয়াতুল_কুরসি';
preg_match('/(#\w+)/u', $hashtag, $matches);
print_r($matches);
?>
And the result is:
Array
(
[0] => #আয়
[1] => #আয়
)
I tried changing the pattern to '/(#\p{L}+)/u', but that didn't help.
The fact is that \w here does not match all diacritics that Bengali characters may contain. You need to allow them all:
preg_match('/#[\w\p{M}]+/u', $hashtag, $matches);
See the PHP demo.

Using preg_match to find all text between brackets

I am trying to search a document for tags, to later replace them.
I am using preg_match, but am having some difficulty.
preg_match('/\[.*\]/', $haystack, $matches);
Searching through the following text
[TODAY_DATE]
Re: Demand for Payment
[ADDRESS]
[ADDRESS_LINE_2]
...etc
print_r($matches);
returns
Array ( [0] => [TODAY_DATE] )
How should I adjust my regex to return all matches?
Use preg_match_all. As the name suggests, it matches more than once.
preg_match_all('/\[.*?\]/', $haystack, $matches);
var_dump($matches);
Also use reluctant quantifier .*? instead of greedy one.

PHP- Regular expression - how to read from Right to left

I have below example
$game = "hello999hello888hello777last";
preg_match('/hello(.*?)last/', $game, $match);
The above code returns 999hello888hello777, what I need is to retrieve the value just before Last, i.e 777. So I need to read regular expression to read from right to left.
$game = strrev($game);
How about that? :D
Then just reverse the regular expression ^__^
Why not just reverse the string? Use PHP's strrev and then just reverse your regular expression.
$game = "hello999hello888hello777last";
preg_match('/tsal(.*?)elloh/', strrev($game), $match);
This will return the last set of digits before the string last
$game = "hello999hello888hello777last";
preg_match('/hello(\d+)last$/', $game, $match);
print_r($match);
Output Example:
Array
(
[0] => hello777last
[1] => 777
)
So you would need $match[1]; for the 777 value
Your problem is that although .* matches reluctantly, i. e. as few characters as possible, it still starts matching right after hello, and since it matches any characters, it will match right across "boundaries" (last and hello in your case).
Therefore you need to be more explicit about the fact that it's not legal to match across boundaries, and that's what lookahead assertions are for:
preg_match('/hello((?:(?!hello|last).)*)last(?!.*(?:hello|last)/', $game, $match);
Now the match between hello and last is prohibited from containing hello and/or last, and it's not allowed to have hello or last after the match.

Having trouble with this regular expression in PHP

I'm trying to run regular expression on the following string with PHP using preg_match_all function
"{{content 1}}{{content 2}}"
The result I'm looking for is array with 2 matches inside {{ and }}
Here is the expression '/\{\{(.+)\}\}/'
I'm suspecting that my expression is too greedy but how to make it less greedy?
You can use the ungreedy modifier ?, like so:
$regex = '/\{\{.*?\}\}/';
New regex will output:
Array
(
[0] => Array
(
[0] => {{content 1}}
[1] => {{content 2}}
)
)
EDIT:
Just remembered another way to do this. You can just add a U (capital u) in the end of your regex string and result will be the same, like so:
$regex = '/\{\{.+\}\}/U';
Also, here is a useful list of regex modifiers.
You can also use the U PCRE modifier (Ungreedy)
Is the regular expression string in a single quote or double quote PHP string? Because if it's in a double quoted string, curly braces include variables so need to be escaped twice.
"/\\{\\{(.+)\\}\\}/"

Regex Search Help

Given a string such as:
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
I want to find every integer within quotes and create an array of all integers found in the string.
End result should be an array like:
Array
(
[0] => 1
[1] => 2
)
I'm guessing you use preg_match() but I have no experience with regular expressions :(
How about this:
$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
print_r(array_values(unserialize($str)));
Not a regex, same answer.
This works because the string you have is a serialized PHP array. Using a regex would be the wrong way to do this.
The regex (in a program) would look like this:
$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
preg_match_all('/"(\d+)"/', $str, $matches);
print_r($matches[1]);

Categories