Im trying to catch all urls inside a #font-face but $matches creates duplicates, which it shouldn't. I really can't figure out at all why this happens. I created the regex with https://regex101.com/ and it works well but it does not work when I put it in a php document myself. Any help is really appreciated!
My php version is 7.1 if that helps!
<?php
$re = '/url\s*\(\s*[\'"]?(?!(((?:https?:)?\/\/)|(?:data\:?:)))([^\'"\)]+)[\'"]?\s*\)/';
$str = '#font-face{font-family:Poiret One;font-style:normal;font-weight:400;src:local(\'PoiretOne-Regular\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf) format(\'truetype\')}#font-face{font-family:Open sans;font-style:normal;font-weight:400;src:local(\'OpenSans-Regular\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf) format(\'truetype\')}#font-face{font-family:Open sans;font-weight:300;src:local(\'OpenSans-Light\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf) format(\'truetype\')}#font-face{font-family:Dancing Script;font-weight:400;font-style:normal;src:local(\'dancing-script_regular\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf) format(\'truetype\')}#font-face{font-family:Josefin Slab;font-weight:400;font-style:normal;src:local(\'JosefinSlab-Regular\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf) format(\'truetype\')}#font-face{font-family:Josefin Slab;font-weight:400;font-style:italic;src:local(\'JosefinSlab-Italic\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2) format(\'woff2\'),url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff) format(\'woff\'),url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf) format(\'truetype\')}';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
print_r($matches);
If you ctrl+f and write divi-child you can see that it's stated 18 times in the $str but $matches is 36 times:
Array ( [0] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2 ) [1] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff ) [2] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf ) [3] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2 ) [4] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff ) [5] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/OpenSans-Regular.ttf ) [6] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff2 ) [7] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/opensans-light-webfont_webfont.woff ) [8] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/OpenSans-Light.ttf ) [9] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff2 ) [10] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancingscript-regular_webfont.woff ) [11] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/dancing-script_regular_webfont.ttf ) [12] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff2 ) [13] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-regular_webfont.woff ) [14] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/JosefinSlab-Regular.ttf ) [15] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff2 ) [16] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/josefinslab-italic_webfont.woff ) [17] => Array ( [0] => url(/wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf) [1] => [2] => [3] => /wp-content/themes/Divi-Child/fonts/JosefinSlab-Italic.ttf ) )
I just ran your code.
I got an array of 18 elements.
The reason your Ctrl+F is returning two instances of "divi-child" is because it is present in the full contents match, and the smaller match you specify with brackets.
array (size=18)
0 =>
array (size=4)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
1 =>
array (size=4)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
<snip>
Also, when debugging, var_dump() is a better function - gives you more information about the variable contents.
Suggested Solution for User
(Rather than trying to squeeze this into comments...)
Change the Regular Expression to
$re = '/url\s*\(\s*[\'"]?(?!(?:(?:https?:)?\/\/)|(?:data\:?:))([^\'"\)]+)[\'"]?\s*\)/';
Change the parameters sent to the preg_match_all() function to
preg_match_all($re, $str, $matches);
The returned value will be an array with two elements (each arrays in themselves). The first element will be an array of the complete matches to your regular expression. The second element will the the portion of the match which we are capturing with the brackets.
array (size=2)
0 =>
array (size=18)
0 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2)' (length=72)
1 => string 'url(/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff)' (length=71)
2 => string 'url(/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf)' (length=62)
3 => string 'url(/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2)' (length=79)
<snip>
1 =>
array (size=18)
0 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff2' (length=67)
1 => string '/wp-content/themes/Divi-Child/fonts/poiretone-regular_webfont.woff' (length=66)
2 => string '/wp-content/themes/Divi-Child/fonts/PoiretOne-Regular.ttf' (length=57)
3 => string '/wp-content/themes/Divi-Child/fonts/opensans-regular-webfont_webfont.woff2' (length=74)
<snip>
Related
I am attempting to convert an associative array to a 2D array to allow me to export it to Google Sheets. I've figured out a simplistic solution that works as follows:
$headers = $data["resultSets"][0]["headers"];
$rowSet0 = $data["resultSets"][0]["rowSet"][0];
$rowSet1 = $data["resultSets"][0]["rowSet"][1];
$hackresults = array_map(null, $headers, $rowSet0, $rowSet1);
This produces the following:
(
[0] => Array
(
[0] => SEASON_ID
[1] => 22017
[2] => 22017
)
[1] => Array
(
[0] => Player_ID
[1] => 203954
[2] => 203954
)
[2] => Array
(
[0] => Game_ID
[1] => 0021701118
[2] => 0021701105
)
[3] => Array
(
[0] => GAME_DATE
[1] => MAR 28, 2018
[2] => MAR 26, 2018
)
[4] => Array
(
[0] => MATCHUP
[1] => PHI vs. NYK
[2] => PHI vs. DEN
)
[5] => Array
(
[0] => WL
[1] => W
[2] => W
)
[6] => Array
(
[0] => MIN
[1] => 9
[2] => 27
)
[7] => Array
(
[0] => FGM
[1] => 2
[2] => 6
)
[8] => Array
(
[0] => FGA
[1] => 6
[2] => 12
)
[9] => Array
(
[0] => FG_PCT
[1] => 0.333
[2] => 0.5
)
[10] => Array
(
[0] => FG3M
[1] => 0
[2] => 0
)
[11] => Array
(
[0] => FG3A
[1] => 1
[2] => 1
)
[12] => Array
(
[0] => FG3_PCT
[1] => 0
[2] => 0
)
[13] => Array
(
[0] => FTM
[1] => 1
[2] => 8
)
[14] => Array
(
[0] => FTA
[1] => 2
[2] => 10
)
[15] => Array
(
[0] => FT_PCT
[1] => 0.5
[2] => 0.8
)
[16] => Array
(
[0] => OREB
[1] => 2
[2] => 1
)
[17] => Array
(
[0] => DREB
[1] => 1
[2] => 12
)
[18] => Array
(
[0] => REB
[1] => 3
[2] => 13
)
[19] => Array
(
[0] => AST
[1] => 0
[2] => 2
)
[20] => Array
(
[0] => STL
[1] => 0
[2] => 1
)
[21] => Array
(
[0] => BLK
[1] => 0
[2] => 2
)
[22] => Array
(
[0] => TOV
[1] => 1
[2] => 4
)
[23] => Array
(
[0] => PF
[1] => 1
[2] => 5
)
[24] => Array
(
[0] => PTS
[1] => 5
[2] => 20
)
[25] => Array
(
[0] => PLUS_MINUS
[1] => 7
[2] => 20
)
[26] => Array
(
[0] => VIDEO_AVAILABLE
[1] => 1
[2] => 1
)
)
This is the output I'm looking for, but there are 27 "rowSet"s, and it seems there must be a recursive way of performing this task.
I've looked at a number of custom array_map_recursive style functions but haven't had any success. Apologies and thanks in advance, I am a terrible novice coder!
You can use argument unpacking.
With the ... operator, you can use all the elements under $data["resultSets"][0]["rowSet"] as additional arguments to array_map.
$headers = $data["resultSets"][0]["headers"];
$rowSets = $data["resultSets"][0]["rowSet"];
$results = array_map(null, $headers, ...$rowSets);
(This isn't recursion, but I think it does what you're trying to do.)
I want to parse a multidimensional array and store all the matching keys into their own array.
Input Array:
array (
[Items] => Array (
[0] => AS1585.AGG
[1] => AS1585.AGG
[2] => ZBRK-1776
[3] => ZBRK-2076
[4] => ZBRK-3001
)
[Qty2] => Array (
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 6
)
[Cost] => Array (
[0] => $351.00
[1] => $351.00
[2] => $437.00
[3] => $380.00
[4] => $358.00
)
[TotalCubes2] => Array (
[0] => 168.40
[1] => 168.40
[2] => 186.05
[3] => 161.25
[4] => 140.70
)
[TotalCost2] => Array (
[0] => $1755.00
[1] => $1755.00
[2] => $2185.00
[3] => $1900.00
[4] => $2148.00
)
)
Desired output:
Array (
[Items] => AS1585.AGG
[Qty2] => 5
[Cost] => $351.00
[TotalCubes2] => 168.40
[TotalCost2] => $1755.00
)
I have an array items following.
Array
(
[0] => Array
(
[0] => 2
[field_id] => 2
[1] => Photometric Interpretation
[title] => Photometric Interpretation
[2] => text
[field_type] => text
)
[1] => Array
(
[0] => 3
[field_id] => 3
[1] => Make
[title] => Make
[2] => text
[field_type] => text
)
[2] => Array
(
[0] => 4
[field_id] => 4
[1] => Model
[title] => Model
[2] => text
[field_type] => text
)
[3] => Array
(
[0] => 5
[field_id] => 5
[1] => Strip Offsets
[title] => Strip Offsets
[2] => text
[field_type] => text
)
[4] => Array
(
[0] => 6
[field_id] => 6
[1] => Samples Per Pixel
[title] => Samples Per Pixel
[2] => text
[field_type] => text
)
[5] => Array
(
[0] => 7
[field_id] => 7
[1] => Rows Per Strip
[title] => Rows Per Strip
[2] => text
[field_type] => text
)
[6] => Array
(
[0] => 8
[field_id] => 8
[1] => Software
[title] => Software
[2] => text
[field_type] => text
)
[7] => Array
(
[0] => 9
[field_id] => 9
[1] => Exposure Time
[title] => Exposure Time
[2] => text
[field_type] => text
)
)
I need to sort above array's field_id INDEX according to this following array's VALUE
Array
(
[0] => 7
[1] => 3
[2] => 4
[3] => 5
[4] => 2
[5] => 6
)
OR to following string
7,3,4,5,2,6
I have tried to sort array using uksort() and uasort().
Usual foreach makes the desired result
$index = array_flip([7,3,4,5,2,6]);
foreach($arr as $item)
$res[$index[$item['field_id']]] = $item;
I've got this simple code to test the output of token_get_all...
$arr = token_get_all("<?php $array=array(1,2,3); foreach($array as $key => $value) print($value); ?>");
print("<pre>");
print_r($arr);
print("</pre>");
But what ends up being displayed is this:
Array
(
[0] => Array
(
[0] => 372
[1] => 1
)
[1] => =
[2] => Array
(
[0] => 362
[1] => array
[2] => 1
)
[3] => (
[4] => Array
(
[0] => 305
[1] => 1
[2] => 1
)
[5] => ,
[6] => Array
(
[0] => 305
[1] => 2
[2] => 1
)
[7] => ,
[8] => Array
(
[0] => 305
[1] => 3
[2] => 1
)
[9] => )
[10] => ;
[11] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[12] => Array
(
[0] => 322
[1] => foreach
[2] => 1
)
[13] => (
[14] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[15] => Array
(
[0] => 326
[1] => as
[2] => 1
)
[16] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[17] => Array
(
[0] => 360
[1] => =>
[2] => 1
)
[18] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[19] => )
[20] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[21] => Array
(
[0] => 266
[1] => print
[2] => 1
)
[22] => (
[23] => )
[24] => ;
[25] => Array
(
[0] => 375
[1] =>
[2] => 1
)
[26] => Array
(
[0] => 374
[1] => ?>
[2] => 1
)
)
From everything I've read about token_get_all, I'd expect the [0] key of these arrays to be the token names. What's going on with my code/server that I'm getting this instead?
I've also tried doing:
$arr = token_get_all(file_get_contents('someOtherValidPHPFile.php'));
And I get the same kind of result.
I'm using PHP version 5.4.19
Yes the token type is on index 0.
This is just a numeric value which identifies the token type. You can then compare them against the following list of token types: List of Parser Tokens
You can get the token name by using the token_name() function.
Tokens are defined as constants. E.g. the constant is named T_ARRAY and its value is 362. You can compare the tokens to that constant:
if ($token[0] == T_ARRAY) ...
If you want to get the readable name, use token_name.
i have the following array and want to get rid/remove the empty array and rearrange it in an order.can anyone help me please.
Array
(
[ufile] => Array
(
[name] => Array
(
[0] => chicken soup.jpg
[1] =>
[2] => hot n sour sup.jpg
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[type] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[error] => Array
(
[0] => 1
[1] => 4
[2] => 1
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
)
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
)
)
)
I believe this will cleanup your array: remove empty elements (those that evaluate to empty, eg "" and 0 equally), remove duplicate elements, and sort it.
$cleaned = array_map('array_filter', $array_to_be_cleaned);
$cleaned = array_map('array_unique', $cleaned);
$cleaned = array_map('sort', $cleaned);
To filter out the empty elements of the array, check out array_filter.
To sort the elements, check out sort (or refer to this list of sorting functions to see what meets your needs)
$newarray = array_filter($myarray);
sort($newarray);
This will take the array you pass it (ie. $myarray) and it will strip out any empty values, then it will store the results to $newarray. After that, sort will organize the remaining values from least to greatest.
unset( $var['ufile']['type'] );
unset( $var['ufile']['tmp_name'] );
To sort, use sort(), usort(), or any other you want. Here's a good comparison of the different sort methods.
Try array_filter($array). It will remove all NULL elements and return the cleared array.