preg_match and a troublesome regex - php

Could some one tell me why
//string
$content = 'random ${one.var} ${two.var} random';
//match
preg_match('/(?:(?<=\$\{))([\w.]+){1}(?=\})/i', $content, $matches);
is returning
print_R($matches);
//output
array(
[0]=>one.var
[1]=>one.var
);
What i want is
array(
[0]=>one.var
[1]=>two.var
);

Both the whole regex (0) as the inner capture () (1) match the same thing, so that part of the match makes sense. You probably want preg_match_all, which captures all matches...
preg_match_all('/(?<=\$\{)[\w.]+(?=\})/i', $content, $matches);

You should use preg_match_all to perform a global regex search, also - i think you can simplify the pattern this way:
preg_match_all('/\$\{(.*?)\}/', $content, $matches)

just try preg_match_all()
http://php.net/preg_match_all Searches subject for all matches
http://php.net/preg_match Searches subject for a match

Related

Find a pattern in a string

I am trying to detect a string inside the following pattern: [url('example')] in order to replace the value.
I thought of using a regex to get the strings inside the squared brackets and then another to get the text inside the parenthesis but I am not sure if that's the best way to do it.
//detect all strings inside brackets
preg_match_all("/\[([^\]]*)\]/", $text, $matches);
//loop though results to get the string inside the parenthesis
preg_match('#\((.*?)\)#', $match, $matches);
To match the string between the parenthesis, you might use a single pattern to get a match only:
\[url\(\K[^()]+(?=\)])
The pattern matches:
\[url\( Match [url(
\K Clear the current match buffer
[^()]+ Match 1+ chars other than ( and )
(?=\)]) Positive lookahead, assert )] to the right
See a regex demo.
For example
$re = "/\[url\(\K[^()]+(?=\)])/";
$text = "[url('example')]";
if (preg_match($re, $text, $match)) {
var_dump($match[0]);;
}
Output
string(9) "'example'"
Another option could be using a capture group. You can place the ' inside or outside the group to capture the value:
\[url\(([^()]+)\)]
See another regex demo.
For example
$re = "/\[url\(([^()]+)\)]/";
$text = "[url('example')]";
if (preg_match($re, $text, $match)) {
var_dump($match[1]);;
}
Output
string(9) "'example'"

Find next word after colon in regex

I am getting a result as a return of a laravel console command like
Some text as: 'Nerad'
Now i tried
$regex = '/(?<=\bSome text as:\s)(?:[\w-]+)/is';
preg_match_all( $regex, $d, $matches );
but its returning empty.
my guess is something is wrong with single quotes, for this i need to change the regex..
Any guess?
Note that you get no match because the ' before Nerad is not matched, nor checked with the lookbehind.
If you need to check the context, but avoid including it into the match, in PHP regex, it can be done with a \K match reset operator:
$regex = '/\bSome text as:\s*'\K[\w-]+/i';
See the regex demo
The output array structure will be cleaner than when using a capturing group and you may check for unknown width context (lookbehind patterns are fixed width in PHP PCRE regex):
$re = '/\bSome text as:\s*\'\K[\w-]+/i';
$str = "Some text as: 'Nerad'";
if (preg_match($re, $str, $match)) {
echo $match[0];
} // => Nerad
See the PHP demo
Just come from the back and capture the word in a group. The Group 1, will have the required string.
/:\s*'(\w+)'$/

Using preg_match to match this specific pattern

I have a preg_match matching for specific patterns, but it's just not matching the pattern I'm trying to match. What am I doing wrong?
<?php
$string = "tell me about cats";
preg_match("~\b(?:tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+ ){1,2})$~", $string, $match);
print_r($match);
?>
Expected Result:
array(0 => tell me about 1 => cats)
Actual Result:
array()
You are having an extra space in (but there are no spaces after cat making the entire regex to fail)
((?:[a-z]+ ){1,2})
^^
||
here
also, you don't have capturing group for first part (due to (?:..)). Make a capturing group and make the spaces optional using ? (if you want to capture at most two words)
\b(tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+){1,2} ?)$
Regex Demo
PHP Code
$string = "tell me about cats";
preg_match("~\b(tell me about|you know(?: of| about)?|what do you think(?: of| about)?|(?:what|who) is|(?:whats|whos)) ((?:[a-z]+ ?){1,2})$~", $string, $match);
print_r($match);
NOTE :- $match[1] and $match[2] will contain your result. $match[0] is reserved for entire match found by the regex in the string.
Ideone Demo

PHP exploding url from text, possible?

i need to explode youtube url from this line:
[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]
It is possible? I need to delete [embed] & [/embed].
preg_match is what you need.
<?php
$str = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
preg_match("/\[embed\](.*)\[\/embed\]/", $str, $matches);
echo $matches[1]; //https://www.youtube.com/watch?v=L3HQMbQAWRc
$string = '[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]';
$string = str_replace(['[embed]', '[/embed]'], '', $string);
See str_replace
why not use str_replace? :) Quick & Easy
http://php.net/manual/de/function.str-replace.php
Just for good measure, you can also use positive lookbehind's and lookahead's in your regular expressions:
(?<=\[embed\])(.*)(?=\[\/embed\])
You'd use it like this:
$string = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
$pattern = '/(?<=\[embed\])(.*)(?=\[\/embed\])/';
preg_match($pattern, $string, $matches);
echo $match[1];
Here is an explanation of the regex:
(?<=\[embed\]) is a Positive Lookbehind - matches something that follows something else.
(.*) is a Capturing Group - . matches any character (except a newline) with the Quantifier: * which provides matches between zero and unlimited times, as many times as possible. This is what is matched between the groups prior to and after. This are the droids you're looking for.
(?=\[\/embed\]) is a Positive Lookahead - matches things that come before it.

How to match full words?

I use a simple preg_match_all to find the occurrence of a list of words in a text.
$pattern = '/(word1|word2|word3)/';
$num_found = preg_match_all( $pattern, $string, $matches );
But this also match subset of words like abcword123. I need it to find word1, word2 and word3 when they're occurring as full words only. Note that this doesn't always mean that they're separated by spaces on both sides, it could be a comma, semi-colon, period, exclamation mark, question mark, or another punctuation.
IF you are looking to match "word1", "word2", "word3" etc only then using in_array is always better. Regex are super powerful but it takes a lot of cpu power also. So try to avoid it when ever possible
$words = array ("word1", "word2", "word3" );
$found = in_array ($string, $words);
check PHP: in_array - Manual for more information on in_array
And if you want to use regex only try
$pattern = '/^(word1|word2|word3)$/';
$num_found = preg_match_all( $pattern, $string, $matches );
And if you want to get something like "this statement has word1 in it", then use "\b" like
$pattern = '/\b(word1|word2|word3)\b/';
$num_found = preg_match_all( $pattern, $string, $matches );
More of it here PHP: Escape sequences - Manual search for \b
Try:
$pattern = '/\b(word1|word2|word3)\b/';
$num_found = preg_match_all( $pattern, $string, $matches );
You can use \b to match word boundaries. So you want to use /\b(word1|word2|word3)\b/ as your regex.

Categories