This is my input content:
$input = 'Hi there,
You recently ... {{client.events.add_to_cart.product_name}}, ...
..
Also
...
{{client.events.purchase.product_name}}
';
and this is regex example:
$regex = "'(?:\{\{client\.events\.)\w+(?:\.)\w+(?:\}\})'";
preg_match_all($regex, $input, $matches);
This is the $matches content:
array:1 [
0 => array:2 [
0 => "{{client.events.add_to_cart.product_name}}"
1 => "{{client.events.purchase.product_name}}"
]
]
Not bad, but I'd like somehow to pick "add_to_cart" and "product_name" as well. I can do it with explode() function, but wonder if there is a way to do this by regex only once.
Also, I wonder if it is possible to have 'target' these too:
{{client.events.add_to_cart.product_name}}
{{client.events.add_to_cart.0.product_name}}
{{client.events.add_to_cart.1.product_name}}
with and without numbers. And also knowing the number.
Thanks.
This will do it: (\{\{client\.events\.(?:purchase|(add_to_cart))(?:\.\d)?\.(.*)\}\})
See: https://regex101.com/r/EKAJsG/1
Related
I have string :
$string = 'ABCCDF[GH]IJJ[KLM]';
How to i get string 'GH' and 'KLM' in php
I think i will use preg_split, but I know not more about reg. Plz help me.
Try this;
>>> $string = 'ABCCDF[GH]IJJ[KLM]';
=> "ABCCDF[GH]IJJ[KLM]"
>>> preg_match_all('/\[(\w+)\]/', $string, $matches);
=> 2
$matches will look like below.
[
[
"[GH]",
"[KLM]",
],
[
"GH",
"KLM",
],
]
I have this as an input to my command line interface as parameters to the executable:
-Parameter1=1234 -Parameter2=38518 -param3 "Test \"escaped\"" -param4 10 -param5 0 -param6 "TT" -param7 "Seven" -param8 "secret" "-SuperParam9=4857?--SuperParam10=123"
What I want to is to get all of the parameters in a key-value / associative array with PHP like this:
$result = [
'Parameter1' => '1234',
'Parameter2' => '1234',
'param3' => 'Test \"escaped\"',
'param4' => '10',
'param5' => '0',
'param6' => 'TT',
'param7' => 'Seven',
'param8' => 'secret',
'SuperParam9' => '4857',
'SuperParam10' => '123',
];
The problem here lies at the following:
parameter's prefix can be - or --
parameter's glue (value assignment operator) can be either an = sign or a whitespace ' '
some parameters may be inside a quote block and can also have different, both separators and glues and prefixes, ie. a ? mark for the separator.
So far, since I'm really bad with RegEx, and still learning it, is this:
/(-[a-zA-Z]+)/gui
With which I can get all the parameters starting with an -...
I can go to manually explode the entire thing and parse it manually, but there are way too many contingencies to think about.
You can try this that uses the branch reset feature (?|...|...) to deal with the different possible formats of the values:
$str = '-Parameter1=1234 -Parameter2=38518 -param3 "Test \"escaped\"" -param4 10 -param5 0 -param6 "TT" -param7 "Seven" -param8 "secret" "-SuperParam9=4857?--SuperParam10=123"';
$pattern = '~ --?(?<key> [^= ]+ ) [ =]
(?|
" (?<value> [^\\\\"]*+ (?s:\\\\.[^\\\\"]*)*+ ) "
|
([^ ?"]*)
)~x';
preg_match_all ($pattern, $str, $matches);
$result = array_combine($matches['key'], $matches['value']);
print_r($result);
demo
In a branch reset group, the capture groups have the same number or the same name in each branch of the alternation.
This means that (?<value> [^\\\\"]*+ (?s:\\\\.[^\\\\"]*)*+ ) is (obviously) the value named capture, but that ([^ ?"]*) is also the value named capture.
You could use
--?
(?P<key>\w+)
(?|
=(?P<value>[^-\s?"]+)
|
\h+"(?P<value>.*?)(?<!\\)"
|
\h+(?P<value>\H+)
)
See a demo on regex101.com.
Which in PHP would be:
<?php
$data = <<<DATA
-Parameter1=1234 -Parameter2=38518 -param3 "Test \"escaped\"" -param4 10 -param5 0 -param6 "TT" -param7 "Seven" -param8 "secret" "-SuperParam9=4857?--SuperParam10=123"
DATA;
$regex = '~
--?
(?P<key>\w+)
(?|
=(?P<value>[^-\s?"]+)
|
\h+"(?P<value>.*?)(?<!\\\\)"
|
\h+(?P<value>\H+)
)~x';
if (preg_match_all($regex, $data, $matches)) {
$result = array_combine($matches['key'], $matches['value']);
print_r($result);
}
?>
This yields
Array
(
[Parameter1] => 1234
[Parameter2] => 38518
[param3] => Test \"escaped\"
[param4] => 10
[param5] => 0
[param6] => TT
[param7] => Seven
[param8] => secret
[SuperParam9] => 4857
[SuperParam10] => 123
)
I have an array like this
0 => array:4 [▼
"Name" => "Aroma Therapy - 45 minutes"
"ID" => "1000000015"
"Price" => "50.00"
"Category" => "Aroma"
]
1 => array:4 [▼
"Name" => "Aroma Therapy - 60 Minutes"
"ID" => "0000000003"
"Price" => "100.00"
"Category" => "Aroma"
What I am trying to achieve is that, whenever a user searches for 'therapy' or 'aroma', I want to match with this array's name and return it's price and ID.
I tried using strpos() strstr() and regular expressions as well, I can find if the search matches or not but I cannot get the functionality of returning the array when matched.
So, if a user searches for 'therapy', I want to return the two arrays above, in a variable called $result. So I can access it's name, ID and price like this
$result->Name, $result->ID, $result->Price
Any ideas on how to achieve this kind of functionality?
Using array_filter and stripos(), you can get array items
stripos — Find the position of the first occurrence of a
case-insensitive substring in a string
array_filter — Filters elements of an array using a callback
function
// keyword to be searched
$keyword = 'aroma';
// you will get all array items of matched
$result_array = array_filter($array, function($item) use ($keyword) {
return ( stripos($item['Name'], $keyword) !== false );
});
// to convert to object
$object = json_decode( json_encode($result_array) );
i got an array
$array = [
"a" => "c",
"b" => "d",
"c" => "a",
"d" => "b",
];
and a string $text = "dcab";
How can i replace with my array elements each letter on my string, im try to figure out by steps but not luck,
1.- explode my string
2.- for each letter str_replace,
output will be: bacd
thank for help.
Well you can't use str_replace() because of the big warning that it shows in the documentation:
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.
Then take a look at the strtr() function
echo strtr($text, $array);
I'm trying to match two types of strings using the preg_match function in PHP which could be the following.
'_mything_to_newthing'
'_onething'
'_mything_to_newthing_and_some_stuff'
In the third one above, I only want the "mything" and "newthing" so everything that comes after the third part is just some optional text the user could add. Ideally out of the regex would come in the cases of above;
'mything', 'newthing'
'onething'
'mything', 'newthing'
The patterns should match a-zA-Z0-9 if possible :-)
My regex is terrible, so any help would be appreciated!
Thanks in advanced.
Assuming you're talking about _ deliminated text:
$regex = '/^_([a-zA-Z0-9]+)(|_to_([a-zA-Z0-9]+).*)$/';
$string = '_mything_to_newthing_and_some_stuff';
preg_match($regex, $string, $match);
$match = array(
0 => '_mything_to_newthing_and_some_stuff',
1 => 'mything',
2 => '_to_newthing_and_some_stuff',
3 => 'newthing',
);
As far as anything farther, please provide more details and better sample text/output
Edit: You could always just use explode:
$parts = explode('_', $string);
$parts = array(
0 => '',
1 => 'mything',
2 => 'to',
3 => 'newthing',
4 => 'and',
5 => 'some',
6 => 'stuff',
);
As long as the format is consistent, it should work well...