I can`t figure out the following expression:
preg_match_all('/[(?P<slug>\w+\-)\-(?P<flag>(m|t))\-(?P<id>\d+)]+/', $slugs, $matches);
My $slugs variable is something like this:
article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/
Your expression looks like an attempt to split up the path elements into slug, flag and id parts. It fails as the brackets [ ... ] is used to match characters but here it seems to be used to keep things together, like parentheses. It also fails to get the slug part right, as it does not allow for more than one series of word \w and dash - characters. I.e. that part matches 'article-' but not 'article-slug-one-'.
Maybe this is what you want?
$slugs = 'article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/';
preg_match_all('/(?P<slug>[\w-]+)\-(?P<flag>[mt])\-(?P<id>\d+)/', $slugs, $matches);
echo "First slug : " . $matches['slug'][0], PHP_EOL;
echo "Second flag: " . $matches['flag'][1], PHP_EOL;
echo "Third ID : " . $matches['id'][2], PHP_EOL;
print_r($matches);
Output:
First slug : article-slug-one
Second flag: t
Third ID : 111581
Array
(
[0] => Array
(
[0] => article-slug-one-m-111617
[1] => article-slug-two-t-111611
[2] => article-slug-three-t-111581
[3] => article-slug-four-m-111609
)
[slug] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[1] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[flag] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[2] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[id] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
[3] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
)
Related
So this is how my Array ($dataArray) looks oks like:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 10:45:43
[2] => 237
[3] => Some text from
[4] => Client 1
)
[2] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
[3] => Array
(
[0] => 2021-12-01
[1] => 11:53:03
[2] => 44
[3] => anonymous
[4] => Client 1
)
I need to Loop trough it to search the Client Names, and if i find the matching name in the Element 4 then delete the entire Array.
$ExportKDname = "Client 1"
foreach($dataArray as $key => $sub_array) {
if($sub_array[4] == $ExportKDname) {
unset($dataArray[$key]);
break;
}
}
print_r($dataArray);
But with this code none of the arrays will be deleted. And I just can not find the right way to do it.
The Final array what I need to look like if we find the "Client 1" in the array would be like this:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
In the if condition you are saying "if u match with $sub_arr[4] == $ExportKDname unset it and stop the loop". the machine doing that. when it matched first time it removes and stoping. If u wanna delete all match do not write break; let it continue. So delete or make it comment break; line.
You can array_filter your variable and check if value is in_array.
With PHP 7.4+ syntax it should look like this:
$result = array_filter($dataArray, fn ($innerArray) => !in_array('Client 1', $innerArray));
My simple loop takes a list of coordinates and placenames, such as this:
(59.436961) (24.753575) (Revel, Estonia) (Born)
(-34.847745) (138.507362) (Port Adelaide) (Disembarked)
(-33.177085) (138.010057) (Port Pirie) (Residence before enlistment)
It preg_splits them into the array $coords, which works great. I then iterate through them in a foreach loop. My question is why does this:
foreach ($coords as &$marker){
$marker = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($coords);
Result in this:
Array
(
[0] => Array
(
[0] => 59.436961
[1] => 24.753575
[2] => Revel, Estonia
[3] => Born
)
[1] => Array
(
[0] => -34.847745
[1] => 138.507362
[2] => Port Adelaide
[3] => Disembarked
)
[2] => Array
(
[0] => -34.847745
[1] => 138.507362
[2] => Port Adelaide
[3] => Disembarked
)
)
(Note that elements [1] and [2] are identical) - but this:
foreach ($coords as $marker){
$marker_array[] = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($marker_array);
Result in this:
Array
(
[0] => Array
(
[0] => 59.436961
[1] => 24.753575
[2] => Revel, Estonia
[3] => Born
)
[1] => Array
(
[0] => -34.847745
[1] => 138.507362
[2] => Port Adelaide
[3] => Disembarked
)
[2] => Array
(
[0] => -33.177085
[1] => 138.010057
[2] => Port Pirie
[3] => Residence before enlistment
)
)
Is there something I'm doing wrong, or don't know about, when I attempt to modify elements directly in the loop?
In PHP 5.6 I do not see the described behaviour. How do you parse the raw list?
When I run:
<?php
$coords = array('(59.436961) (24.753575) (Revel, Estonia) (Born)',
'(-34.847745) (138.507362) (Port Adelaide) (Disembarked)',
'(-33.177085) (138.010057) (Port Pirie) (Residence before enlistment)');
foreach ($coords as &$marker){
$marker = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($d);
it gives me this:
Array
(
[0] => Array
(
[0] => 59.436961
[1] => 24.753575
[2] => Revel, Estonia
[3] => Born
)
[1] => Array
(
[0] => -34.847745
[1] => 138.507362
[2] => Port Adelaide
[3] => Disembarked
)
[2] => Array
(
[0] => -33.177085
[1] => 138.010057
[2] => Port Pirie
[3] => Residence before enlistment
)
)
But I wouldn't recoomend using this crazy construction!
If you parse it from text, then simply use preg_match_all with pattern something like this:
'~\(()\)\s*\(()\)\s*\(()\)\s*\(()\)~mu'
Even if you have data in array, I highly recommned that you use preg_match for predictable data matching indstead of preg_split for this purpose. But noone becamed master at day one...
I am trying to preg_match a url consisting of a category slug, an optional subcategory slug and an option item slug.
It works in all cases, except for the 4th case.
$urls[0] = '/main_cat_slug';
$urls[1] = '/main_cat_slug/';
$urls[2] = '/main_cat_slug/sub_cat_slug';
$urls[3] = '/main_cat_slug/sub_cat_slug/';
$urls[4] = '/main_cat_slug/item.html';
$urls[5] = '/main_cat_slug/sub_cat_slug/item.html';
$regexp = array();
$regexp[] = '/(?:(?<category>[\w]+)/?)'; // Find the main category (is always available)
$regexp[] = '(?:(?<subcategory>[\w]+)/?)?'; // Find an optional sub-category, is not always available
$regexp[] = '(?:(?<item>[\w]+)\.html)?'; // Find an optional item, is not always available (don't catch the extension)
$regexp = implode('', $regexp);
foreach($urls as $index=>$url) {
preg_match("#{$regexp}#i", $url, $matches);
echo '<pre><h1>', $index, '</h1>';
echo $url, '<br />';
echo '<br />';
print_r($matches);
}
In the 4-th case, the category will be found, but the item is empty and the subcategory gets the value op "item".
Could someone help me out, so that the 4-th case will only get a category and an item?
This is the output for above code:
0
/main_cat_slug
Array
(
[0] => /main_cat_slug
[category] => main_cat_slug
[1] => main_cat_slug
)
1
/main_cat_slug/
Array
(
[0] => /main_cat_slug/
[category] => main_cat_slug
[1] => main_cat_slug
)
2
/main_cat_slug/sub_cat_slug
Array
(
[0] => /main_cat_slug/sub_cat_slug
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
)
3
/main_cat_slug/sub_cat_slug/
Array
(
[0] => /main_cat_slug/sub_cat_slug/
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
)
4
/main_cat_slug/item.html
Array
(
[0] => /main_cat_slug/item
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => item
[2] => item
)
5
/main_cat_slug/sub_cat_slug/item.html
Array
(
[0] => /main_cat_slug/sub_cat_slug/item.html
[category] => main_cat_slug
[1] => main_cat_slug
[subcategory] => sub_cat_slug
[2] => sub_cat_slug
[item] => item
[3] => item
)
Kind regards!
Patrick
Description
This regex will pickout the three types of data, using the following rules:
The / is always the first character in the string
The Main_Cat is always first, it follows the first / and continues until the next /
If the first string ends in .html/ then this is a Main_Cat
if the first string ends in .html followed by the end of the string, then this is an item
The Sub_Cat is always second, it follows the second / and continues until the next /
If the second string ends in .html/ then this is a Sub_Cat
if the second string ends in .html followed by the end of the string, then this is an item
The Item type always has an .html suffix
There will never be a / after the Item
the Item type will always be the last field
^\/(?:(?<Main_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Sub_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Item>[^\/\r\n]*?)(?:\.html|$))?
If you're using this expression against individual strings then you can remove the new line characters \r\n. The resulting expression would look like: ^\/(?<Main_Cat>[^\/]*)(?:(?:\/(?![^\/]*\.html)(?<Sub_Cat>[^\/]*))?(?:\/(?<Item>[^\/]*)\.html)?)?.*?$ follows the same rules above. Note the end of line $ forces the test to match your entire string
PHP Code Example:
Source String
/category0.html/subcat/item.html
/item1.html
/category2.html/subcat2.html/item2.html
/category3.html/subcat3.html/
/category4.html/item4.html
/main_cat_slug5.html/
/main_cat_slug6/item6
/main_cat_slug7/sub_cat_slug7.html/
/main_cat_slug8/item8.html
/main_cat_slug9/sub_cat_slug9/item9.html
Code
<?php
$sourcestring="your source string";
preg_match_all('/^\/(?:(?<Main_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Sub_Cat>(?![^\/\r\n]*\.html\s*$)[^\/\r\n]*)\/)?(?:(?<Item>[^\/\r\n]*?)(?:\.html|$))?/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
Matches
$matches Array:
(
[0] => Array
(
[0] => /category0.html/subcat/item.html
[1] => /item1.html
[2] => /category2.html/subcat2.html/item2.html
[3] => /category3.html/subcat3.html
[4] => /category4.html/item4.html
[5] => /main_cat_slug5.html
[6] => /main_cat_slug6
[7] => /main_cat_slug7/sub_cat_slug7.html
[8] => /main_cat_slug8/item8.html
[9] => /main_cat_slug9/sub_cat_slug9/item9.html
)
[Main_Cat] => Array
(
[0] => category0.html
[1] =>
[2] => category2.html
[3] => category3.html
[4] => category4.html
[5] => main_cat_slug5.html
[6] => main_cat_slug6
[7] => main_cat_slug7
[8] => main_cat_slug8
[9] => main_cat_slug9
)
[Sub_Cat] => Array
(
[0] => subcat
[1] =>
[2] => subcat2.html
[3] => subcat3.html
[4] =>
[5] =>
[6] =>
[7] => sub_cat_slug7.html
[8] =>
[9] => sub_cat_slug9
)
[Item] => Array
(
[0] => item
[1] => item1
[2] => item2
[3] =>
[4] => item4
[5] =>
[6] =>
[7] =>
[8] => item8
[9] => item9
)
)
You can try this:
preg_match('~/(?<main_cat>[^/\s]++/?+)(?<sub_cat>[^/\s]++/?+)?'
. '(?>(?<filename>\S+?)\.html)?~', $url, $match);
print_r($match);
Note that you can access easily to the different parts with the named captures (useful to test if there is a subpattern or not.).
Ok, I have tried looking at other answers, but couldn't get mine solved. So here is the code:
{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}
I need to get every second value in the quotes (as the "name" values are constant). I actually worked out that I need to get text between :" and " but i can't manage to write a regex for that.
EDIT: I'm doing preg_match_all in php. And its between :" and ", not " and " as someone else edited.
Why on earth would you attempt to parse JSON with regular expressions? PHP already parses JSON properly, with built-in functionality.
Code:
<?php
$input = '{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}';
print_r(json_decode($input, true));
?>
Output:
Array
(
[chg] => -0.71
[vol] => 40700
[time] => 11.08.2011 12:29:09
[high] => 1.417
[low] => 1.360
[last] => 1.400
[pcl] => 1.410
[turnover] => 56,560.25
)
Live demo.
You may need to escape characters or add a forward slash to the front or back depending on your language. But it's basically:
:"([^"].*?)"
or
/:"([^"].*?)"/
I've test this in groovy as below and it works.
import java.util.regex.*;
String test='{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}'
// Create a pattern to match breaks
Pattern p = Pattern.compile(':"([^"]*)"');
// Split input with the pattern
// Run some matches
Matcher m = p.matcher(test);
while (m.find())
System.out.println("Found comment: "+m.group().replace('"','').replace(":",""));
Output was:
Found comment: -0.71
Found comment: 40700
Found comment: 11.08.2011 12:29:09
Found comment: 1.417
Found comment: 1.360
Found comment: 1.400
Found comment: 1.410
Found comment: 56,560.25
PHP Example
<?php
$subject = '{"chg":"-0.71","vol":"40700","time":"11.08.2011 12:29:09","high":"1.417","low":"1.360","last":"1.400","pcl":"1.410","turnover":"56,560.25"}';
$pattern = '/(?<=:")[^"]*/';
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
Output is:
Array ( [0] => Array ( [0] => Array ( [0] => -0.71 [1] => 8 ) [1] => Array ( [0] => 40700 [1] => 22 ) [2] => Array ( [0] => 11.08.2011 12:29:09 [1] => 37 ) [3] => Array ( [0] => 1.417 [1] => 66 ) [4] => Array ( [0] => 1.360 [1] => 80 ) [5] => Array ( [0] => 1.400 [1] => 95 ) [6] => Array ( [0] => 1.410 [1] => 109 ) [7] => Array ( [0] => 56,560.25 [1] => 128 ) ) )
Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )
Array
(
[0] => Kanya
[1] => Janaye
[2] => Kayne
[3] => Kane
[4] => Kaye
)
Array
(
[0] => ST
[1] => St
[2] => st
[3] => EST
[4] => West
)
I've got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?
So the first would be kanye, then list the contents, etc.
Hope that makes sense. I know it will be a simple bit of code but it's stumping me.
You can use a foreach statement to get the key value pair of the array:
$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
print($key); // "kanye"
print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}
If you just need to get the keys, you can use array_keys
$myArray = array(
"Kanye" => array("Kane", ...)
"West" => array("Wst", ...)
);
print_r(array_keys($myArray));
/*
array (
0 => Kanye
1 => West
)
*/
How about just print_r on the outer array?