I am new to PHP and I have the below code and I basically wish to find all keywords enclosed between
'<#' and '#>'
sample code:
<?php
$subject = "askdbvbaldjbvasdblasdbvl<#2134#>cbkdbskbkabdvb<#213aca4#>";
$pattern = "/(?<=\<\#)(.*?)(?=\#\>)/";
preg_match_all($pattern, $subject, $matches);
echo '<pre>',print_r($matches,true),'</pre>';
?>
now i am expecting a value array like:
Array
(
[0] => Array
(
[0] => 2134
[1] => 213aca4
)
)
But i am getting and output like:
Array
(
[0] => Array
(
[0] => 2134
[1] => 213aca4
)
[1] => Array
(
[0] => 2134
[1] => 213aca4
)
)
can any one tell me why am i getting the second array and how can i get rid of that..
The second array contains the sub-match, or matched group, because you're using a capture group.
Simply remove the parens in your regex:
$pattern = "/(?<=\<\#).*?(?=\#\>)/";
Also, you should be able to use this regex without some escapes:
$pattern = "/(?<=<#).*?(?=#>)/";
Related
I'm trying to find a regex capable of capturing the content of short codes produces in Wordpress.
My short codes have the following structure:
[shortcode name param1="value1" param2="value2" param3="value3"]
The number of parameters is variable.
I need to capture the shortcode name, the parameter name and its value.
The closest results I have achieved is with this:
/(?:\[(.*?)|\G(?!^))(?=[^][]*])\h+([^\s=]+)="([^\s"]+)"/
If I have the following content in the same string:
[specs product="test" category="body"]
[pricelist keyword="216"]
[specs product="test2" category="network"]
I get this:
0=>array(
0=>[specs product="test"
1=> category="body"
2=>[pricelist keyword="216"
3=>[specs product="test2"
4=> category="network")
1=>array(
0=>specs
1=>
2=>pricelist
3=>specs
4=>)
2=>array(
0=>product
1=>category
2=>keyword
3=>product
4=>category)
3=>array(
0=>test
1=>body
2=>216
3=>test2
4=>network)
)
I have tried different regex models but I always end up with the same issue, if I have more than one parameter, it fails to detect it.
Do you have any idea of how I could achieve this?
Thanks
Laurent
You could make use of the \G anchor using 3 capture groups, where capture group 1 is the name of the shortcode, and group 2 and 3 the key value pairs.
Then you can remove the first entry of the array, and remove the empty entries in the 1st, 2nd and 3rd entry.
This is a slightly updated pattern
(?:\[(?=[^][]*])(\w+)|\G(?!^))\h+(\w+)="([^"]+)"
Regex demo | Php demo
Example
$s = '[specs product="test" category="body"]';
$pattern = '/(?:\[(?=[^][]*])(\w+)|\G(?!^))\h+(\w+)="([^"]+)"/';
$strings = [
'[specs product="test" category="body"]',
'[pricelist keyword="216"]',
'[specs product="test2" category="network" key="value"]'
];
foreach($strings as $s) {
if (preg_match_all($pattern, $s, $matches)) {
unset($matches[0]);
$matches = array_map('array_filter', $matches);
print_r($matches);
}
}
Output
Array
(
[1] => Array
(
[0] => specs
)
[2] => Array
(
[0] => product
[1] => category
)
[3] => Array
(
[0] => test
[1] => body
)
)
Array
(
[1] => Array
(
[0] => pricelist
)
[2] => Array
(
[0] => keyword
)
[3] => Array
(
[0] => 216
)
)
Array
(
[1] => Array
(
[0] => specs
)
[2] => Array
(
[0] => product
[1] => category
[2] => key
)
[3] => Array
(
[0] => test2
[1] => network
[2] => value
)
)
I have a string with data that looks like this:
$string = '
foo=bar
badge_name_foo=foo
bar_badge_name=bar
bar=baz
';
I want to match all *_badge_name and badge_name_* strings.
The regex im using is this:
preg_match_all('~(?:(\w+)_)?badge_name(?:_(\w+))?~', $string, $matches, PREG_SET_ORDER);
The result is:
Array
(
[0] => Array
(
[0] => badge_name_foo
[1] =>
[2] => foo
)
[1] => Array
(
[0] => bar_badge_name
[1] => bar
)
)
The *_badge_name is working fine, but on badge_name_* there is every time a empty value? Now how can i remove that with preg_match_all
Expected result should be:
Array
(
[0] => Array
(
[0] => badge_name_foo
[1] => foo
)
[1] => Array
(
[0] => bar_badge_name
[1] => bar
)
)
It seems you need to use BRANCH RESET feature:
Alternatives inside a branch reset group share the same capturing groups. The syntax is (?|regex) where (?| opens the group and regex is any regular expression. If you don't use any alternation or capturing groups inside the branch reset group, then its special function doesn't come into play. It then acts as a non-capturing group.
Use
(?|(\w+)_badge_name|badge_name_(\w+))
^^^
See the regex demo.
PHP demo:
$re = '/(?|(\w+)_badge_name|badge_name_(\w+))/';
$str = 'foo=bar
badge_name_foo=foo
bar_badge_name=bar
bar=baz';
preg_match_all($re, $str, $matches);
print_r($matches);
Result:
Array
(
[0] => Array
(
[0] => badge_name_foo
[1] => bar_badge_name
)
[1] => Array
(
[0] => foo
[1] => bar
)
)
I like to know up to three parts of a string. The string can be either
{identifier}, {identifier:foo}, {identifier:foo|bar} or {identifier|bar}
The string can occur multiple times in $content this is why is use preg_match_all
Currently I have
preg_match_all( '#\{'.preg_quote($identifier).':?([^\}|]+)?\|?([^\}]+)?\}#i', $content, $matches );
which work for the very first all versions except {identifier|bar} but not for the others.
Edit:
$matches should look like this (with a single appearance in $content)
Array
(
[0] => Array
(
[0] => {timezone:foo|bar}
)
[1] => Array
(
[0] => foo
)
[2] => Array
(
[0] => bar
)
)
I have an string which looks like:
aaaaaaaa'bbbbbbbb?'cccccccccccc'ddddddddd'
I would like to get all the matches separate by ' but not the ones escaped with ?'
I managed to do it by using this expression:
(.*?)[^\\?]'
And I tested it on regexpal and seems to work properly.
When trying to apply it to my PHP code by using preg_match I only get the first match.
This is my code:
preg_match ("/(.*?)[^\\?]'/i", $content, $matches);
print_r($matches);
And the result is:
Array
(
[0] => aaaaaaaaaaaaaaaa'
[1] => aaaaaaaaaaaaaaa
)
I'm expecting to get bbbbbbbb?'cccccccccccc' and ddddddddd' as well.
What am I doing wrong? Thanks.
You can use this negative lookbehind regex in preg_match_all:
$s = "aaaaaaaa'bbbbbbbb?'cccccccccccc'ddddddddd'";
if (preg_match_all("/(.*?)(?<!\?)'/", $s , $m ))
print_r($m[1]);
OR using preg_split:
$arr = preg_split("/(?<!\?)'/", $s, -1, PREG_SPLIT_NO_EMPTY);
OUTPUT
Array
(
[0] => aaaaaaaa
[1] => bbbbbbbb?'cccccccccccc
[2] => ddddddddd
)
use preg_match_all() to get all matches instead of one
$content = "aaaaaaaa'bbbbbbbb?'cccccccccccc'ddddddddd'";
preg_match_all("/(.*?)[^\\?]'/i", $content, $matches);
print_r($matches);
result
Array (
[0] =>
Array (
[0] => aaaaaaaa'
[1] => bbbbbbbb?'cccccccccccc'
[2] => ddddddddd'
)
[1] =>
Array (
[0] => aaaaaaa
[1] => bbbbbbbb?'ccccccccccc
[2] => dddddddd
)
)
Simply You can try this
$s = "aaaaaaaa'bbbbbbbb?'cccccccccccc'ddddddddd'";
$arr = preg_split("/[^?]'/", $s, -1, PREG_SPLIT_NO_EMPTY);
Out Put Array
Array
(
[0] => aaaaaaa
[1] => bbbbbbbb?'ccccccccccc
[2] => dddddddd
)
I have some text strings like this
{hello|hi}{there|you}
I want to count the instances of {..anything..}, so in the example above, I would want to return:
hello|hi
there|you
in the matches array created by preg_match_all()
Right now my code looks like:
preg_match_all('/{(.*?)}/', $text,$text_pieces);
And $text_pieces contains:
Array ( [0] => Array ( [0] => {hello|hi} [1] => {there|you} ) [1] => Array ( [0] => hello|hi [1] => there|you ) )
All I need is this:
[0] => hello|hi [1] => there|you
preg_match_all cannot omit the full text matches, only subpattern matches, therefore the only solution is to set $text_pieces to $text_pieces[1] after the function call:
if(preg_match_all('/{(.*?)}/', $text,$text_pieces))
{
$text_pieces = $text_pieces[1];
}