What is error in my code? (php) - php

I write this code to replace right to left and left to right css selectors but when i run this , not replacing any text , and return source code .
<?php
$headerString = 'Content-type: text/plain; charset="utf-8"';
header($headerString);
$file = 'test.css';
$cssFile = file_get_contents($file);
$pattern1 = '/(\.|\#).*(left|right).*[\n| | |\n\n|\n ]\{/';
preg_match_all($pattern1, $cssFile, $matches);
$matches = $matches[0];
$patternT = array();
$replacementT = array();
foreach ($matches as $key1 => $val1) {
$patternT[$key1] = preg_replace(array(
'/\./',
'/\#/',
'/\:/',
'/\,/',
'/\(/',
'/\)/',
'/\//',
'/\%/',
'/\;/',
'/\{/',
'/\}/',
), array(
'\.',
'\#',
'\:',
'\,',
'\(',
'\)',
'\/',
'\%',
'\;',
'\{',
'\}',
), $val1);
}
foreach ($patternT as $key2 => $val2) {
$patternT[$key2] = '/'.$val2.'/';
}
foreach ($matches as $key3 => $val3) {
$replacementT[$key3] = preg_replace(array(
'/right/',
'/left/',
), array(
'123456789right123456789',
'123456789left123456789',
), $val3);
}
foreach ($replacementT as $key4 => $val4) {
$replacementT[$key4] = preg_replace(array(
'/123456789right123456789/',
'/123456789left123456789/',
), array(
'left',
'right',
), $val4);
}
//Work
echo preg_replace($patternT[1], $replacementT[1], $cssFile);
//Not work
echo preg_replace($patternT, $replacementT, $cssFile);
?>

So... wait, you just want to switch left and right with each other?
At its simplest, just do $out = strtr($in,array("left"=>"right","right"=>"left"));
There's a possibility this might interfere with other things - for instance, if you have leftarrow.png in a background image, it would change to rightarrow.png... maybe that's a good thing, but if it's not...
$out = preg_replace_callback("/\b(?:right|left)\b/",function($m) {
if( $m[0] == "left") return "right";
return "left";
},$in);

try using str_replcae function in this case because you know the string.
I am not a great fan of JS that is why i am suggesting this:
str_replace('/\./', '\.', $pattern1);
I hope this helps you.
Do the rest by using loop.

Related

Tree map by categories

I'm trying to build a tree-map from categories.
I have the categories (I have a lot of categories and I want to remove duplicates and show them in a tree-map view)
$cat = array(
"Sneakers/Men",
"Sneakers/Women",
"Accessories/Jewellery/Men",
"Accessories/Jewellery/Women",
"Accessories/Jewellery/Men
");
...and I want them like this
$categories = array(
"Sneakers" => array(
"Men" => array(),
"Women" => array()
),
"Accessories" => array(
"Jewellery" => array(
"Men" => array(),
"Women" => array()
)
)
);
to print them like this
- Sneakers
-- Men
-- Women
- Accessories
-- Jewellery
--- Men
--- Women
Try this:
<?php
$cat = array(
"Sneakers/Men",
"Sneakers/Women",
"Accessories/Jewellery/Men",
"Accessories/Jewellery/Women",
"Accessories/Jewellery/Men
");
function buildTree($categories, $result = []){
$temp = [];
foreach($categories as $categoryString){
$catParts = explode('/',$categoryString);
if(count($catParts) > 1){
$temp[$catParts[0]][] = str_replace($catParts[0].'/','',$categoryString);
} else {
$temp[$catParts[0]] = [];
}
}
foreach($temp as $elemName => $elemVal){
$result[$elemName] = buildTree($elemVal);
}
return $result;
}
var_dump(buildTree($cat));
The most simple way is to use references, like this:
$out = [];
foreach ($cat as $str) {
$lookup =& $out;
foreach (explode("/", $str) as $part) {
$lookup =& $lookup[$part];
if (!isset($lookup)) {
$lookup = [];
}
}
}
$lookup initially refers to the whole expected result, then the reference is extended at each step to follow the path of nested members.
Note that each new member added looks like member-name => [], so that actually even final leaves are arrays: it may seem a bit weird, but is a pretty way to have a reduced code (each member is always ready to receive children).
And it's not a difficulty, though, to use the resulting array to then print it like the OP asked:
function nest_print($src, $level = 0) {
$prefix = '<br />' . str_repeat('- ', ++$level);
foreach ($src as $key => $val) {
echo $prefix . $key;
if ($val) {
nest_print($val, $level);
}
}
}
nest_print($out);
EDIT
Here is an alternate solution, including the count of final leaves, as asked by the OP in his comment:
$out = [];
foreach ($cat as $str) {
$lookup =& $out;
$parts = explode("/", $str);
foreach ($parts as $part) {
$lookup =& $lookup[$part];
if (!isset($lookup)) {
$lookup = [];
}
// when $part is a final leaf, count its occurrences
if ($part == end($parts)) {
$lookup = is_array($lookup) ? 1 : ++$lookup;
}
}
}
(might likely be improved in a more elegant way, though)
And here is how to modify the print-result snippet accordingly:
function nest_print($src, $level = 0) {
$prefix = '<br />' . str_repeat('- ', ++$level);
foreach ($src as $key => $val) {
echo $prefix . $key;
if (is_array($val)) {
nest_print($val, $level);
} else {
echo ': ' . $val;
}
}
}
nest_print($out);

No Data & No Error when using function inside IF Statement

I am having trouble getting my php script to work when using a function in if statements.
The code currently runs, but doesn't give me any information, nor any errors, if I remove the IF'S it works fine, but the project I am working on, this is essential.
Here is my code - I have took out the SQL to save space..
if ($VAR === 'PK%') {
CDB::UseDB('blah', 'blah', 'blah', 'blah');
$sql = "blah blah
";
$lrs = CDB::ExecuteQuery($sql);
if ($lrs) {
$jsonData = convert($lrs);
function convert($lrs)
{
// RE-GET VARIABLE AS IT CAN'T GET IT FROM OUTSIDE OF FUNCTION
$VAR = $_GET['VARIABLE'];
$intermediate = array();
while ($vals = CDB::GetAssoc($lrs)) {
$key = $vals['VAR'];
$y = $vals['MEASURE_1'];
if (!isset($intermediate[$key])) $intermediate[$key] = array();
$intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);
}
$output = array();
foreach ($intermediate as $key => $values) {
$output[] = array(
"key" => $key,
'values' => $values
);
}
return json_encode($output, JSON_NUMERIC_CHECK);
}
}
}
Can anyone shed some light on what I am doing wrong?
You are defining your function in a conditional way. In that case its definition must be processed prior to being called.
I would recommend declaring your function separately and then use it, like:
function convert($lrs) {
// RE-GET VARIABLE AS IT CAN'T GET IT FROM OUTSIDE OF FUNCTION
$VAR = $_GET['VARIABLE'];
$intermediate = array();
while ($vals = CDB::GetAssoc($lrs)) {
$key = $vals['VAR'];
$y = $vals['MEASURE_1'];
if (!isset($intermediate[$key])) $intermediate[$key] = array();
$intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);
}
$output = array();
foreach ($intermediate as $key => $values) {
$output[] = array(
"key" => $key,
'values' => $values
);
}
return json_encode($output, JSON_NUMERIC_CHECK);
}
if ($VAR === 'PK%') {
CDB::UseDB('blah', 'blah', 'blah', 'blah');
$sql = "blah blah";
$lrs = CDB::ExecuteQuery($sql);
if ($lrs) {
$jsonData = convert($lrs);
}
}

Php Changing a consecutive array to a inclusive array

I am working on a latin website that holds some information in a string this way:
nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is
I would like to turn this information into an array like this
array(
'nominative'=>array(
0=>array('us','a','um')
),
'genitive'=>array(
0=>'i'
)
'dative'=>array(
1=>'is'
)
'ablative'=>array(
1=>'is'
)
)
My current code looks like this
//turn into array for each specification
$specs=explode(';',$specificities);
foreach($specs as $spec):
//turn each specification into a consecutive array
$parts=explode(':',$spec);
//turn multiple value into array
foreach($parts as &$value)
if(strpos($value,',')!==false)
$value=explode(',',$value);
//imbricate into one other
while(count($parts)!==1):
$val=array_pop($parts);
$key=array_pop($parts);
if(is_array($key)):
foreach($key as $k)
$parts[$k]=$val;
else:
$parts[$key]=$val;
endif;
endwhile;
endforeach;
I'm stuck. Help.
Edit: Thanks for the quick responses everybody!
The response I preferred was from CaCtus. I modified it to add flexibility but it was the most useful.
For those interested, here is the new code:
$parts=explode(';','nominative:0:er,ra,rum;genitive:0:i;dative,ablative:1:is;root:r;');
if(!empty($parts)&&!empty($parts[0])):
foreach($parts as $part):
$subpart=explode(':',$part);
$keys=explode(',',$subpart[0]);
foreach($keys as $key):
#$vals=(strpos($subpart[2],',')!==false)
?explode(',',$subpart[2])
:$subpart[2];
$final=(is_null($vals))
?$subpart[1]
:array($subpart[1]=>$vals);
endforeach;
endforeach;
endif;
$string = 'nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is';
$finalArray = array();
// Separate each specification
$parts = explode(';', $string);
foreach($parts as $part) {
// Get values : specification name, index and value
$subpart = explode(':', $part);
// Get different specification names
$keys = explode(',', $subpart[0]);
foreach($keys as $key) {
if (preg_match('#,#', $subpart[2])) {
// Several values
$finalValues = explode(',', $subpart[2]);
} else {
// Only one value
$finalValues = $subpart[2];
}
$finalArray[$key] = array(
$subpart[1] => $finalValues
);
}
}
Would something a little simplier work?
function multiexplode ($delimiters,$string) {
$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return $launch; }
$input = "0:us,a,um;genitive:0:i;dative,ablative:1:is";
$exploded = multiexplode(array(",",":",";"),$input);
$test = array(
'nominative'=>array(0=>array($exploded[1],$exploded[2],$exploded[3])),
$exploded[4]=>array(0=>$exploded[6]),
$exploded[7]=>array(1=>$exploded[10]),
$exploded[8]=>array(1=>$exploded[10]) );
print_r($test);
Just another alternative:
$specificities = "nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is";
$specificities = trim($specificities, ";") . ";"; //mandatory a trailing ;
$pattern = "/(?<types>[a-z\,]+):(?<ids>\d):(?<values>.*?);/";
preg_match_all($pattern, $specificities, $matches, PREG_SET_ORDER);
$result = array();
array_map(function($item) use (&$result)
{
$types = explode("," , $item['types']);
if(count($types) == 1)
{
$result[$item['types']] = array(
$item['ids'] => explode("," , $item['values'])
);
}
else
{
foreach ($types as $type)
{
$result[$type] = array(
$item['ids'] => explode("," , $item['values'])
);
}
}
}, $matches);
var_dump($result);

php help constructing part numbers

I need to create a large list of part numbers from instructions for how they can be constructed.
Here are two examples.
APXE-N-300-025-A0-12-ET-OO
APXE-N-300-050-A0-12-ET-OO
Each part number is broken into segments like: 0-1-2-3-4-5-6 and some parts have dependencies where, depending on the value of prior parts, there is a restricted selection for that particular part.
Here's the php I have so far. I have an array that sort of contains the pattern but I'm struggling to create the loop that outputs all the options:
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$parts[3] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('075','150','250','500','1M0','1M7','3M5','7M0')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('250','500','1M0','1M7','3M5','7M0','14M'))
);
$parts[4] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('A0','A1','A2')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('B0','B1','B2','C0')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('D0')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('E0')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('F0')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('G0','G2','G3','G4')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('H0','H1')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('K0')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('I0')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('J0','J1'))
);
$parts[5] = array('12','1L','24','48','AC');
$parts[6] = array('ET','FC','IC','FI','NT');
$parts[7] = array('OO');
foreach($parts as $part)
foreach ($part as $bit){
if(!is_array($bit) $list[] = $bit;
else foreach($bit['values'] as $val) $list[] = $val;
} //??
In the above 'dependIndex'=>'2','dependVal'=>'300' means that for all part numbers with '300' in the 2nd segment, the 3rd segment can have all that array's values.
I'm looking for a PHP function that is flexible enough to handle it when there are varying number of segments. Also the dependencies will change. So in the above example 4 could depend on 3 and 6 could depend on 1.
Can anyone say recursion? lol
Any help is greatly appreciated.
I saw your plea for help on Facebook pointing to this post and decided to take a whack at this.
I changed the way your dependencies are listed, but I believe this to work (despite it being a bit rushed and brute force-like). Hopefully it will give you a good place to start.
/*
* The following arrays should be formatted thusly:
* $exclusionlist[indexOfDeterminingColumn][indexOfDependentColumn] = array(
* 'ValueOfDeterminingColumn' => array('list', 'of', 'valid', 'values')
* );
*/
$dependencyList[3][2] = array(
'300' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'400' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'600' => array('1M7','3M5'),
'700' => array('200','250','500','1M0','1M7','3M5'),
'800' => array('075','150','200','250','500','1M0','1M7','3M5'),
'900' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'1G4' => array('075','150','250','500','1M0','1M7','3M5','7M0'),
'1G8' => array('250','500','1M0','1M7','3M5','7M0','14M'),
'2G0' => array('250','500','1M0','1M7','3M5','7M0','14M'),
'2G5' => array('250','500','1M0','1M7','3M5','7M0','14M')
);
$dependencyList[4][2] = array(
'300' => array('A0','A1','A2'),
'400' => array('B0','B1','B2','C0'),
'600' => array('D0'),
'700' => array('E0'),
'800' => array('F0'),
'900' => array('G0','G2','G3','G4'),
'1G4' => array('H0','H1'),
'1G8' => array('K0'),
'2G0' => array('I0'),
'2G5' => array('J0','J1')
);
$dependencyList[5][1] = array(
'N' => array('12'),
'P' => array('1L'),
'S' => array('24')
);
// $segments contains lists of all valid values for each segment of the part number
$segments[0] = array('APXE');
$segments[1] = array('N','P',"S");
$segments[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$segments[3] = array('025','050','075','14M','150','1M0','1M7','200','250','3M5','500','7M0');
$segments[4] = array('A0','A1','A2','B0','B1','B2','C0','D0','E0','F0','G0','G2','G3','G4','H0','H1','I0','J0','J1','K0');
$segments[5] = array('12','1L','24','48','AC');
$segments[6] = array('ET','FC','IC','FI','NT');
$segments[7] = array('OO');
$partNumbers = array();
// Loop through each segment's array
foreach($segments[0] as $segment0)
if(!testForExclusion(${'segment'.key($dependencyList[0])}, key($dependencyList[0]), $segment0, 0, $dependencyList))
foreach($segments[1] as $segment1)
if(!testForExclusion(${'segment'.key($dependencyList[1])}, key($dependencyList[1]), $segment1, 1, $dependencyList))
foreach($segments[2] as $segment2)
if(!testForExclusion(${'segment'.key($dependencyList[2])}, key($dependencyList[2]), $segment2, 2, $dependencyList))
foreach($segments[3] as $segment3)
if(!testForExclusion(${'segment'.key($dependencyList[3])}, key($dependencyList[3]), $segment3, 3, $dependencyList))
foreach($segments[4] as $segment4)
if(!testForExclusion(${'segment'.key($dependencyList[4])}, key($dependencyList[4]), $segment4, 4, $dependencyList))
foreach($segments[5] as $segment5)
if(!testForExclusion(${'segment'.key($dependencyList[5])}, key($dependencyList[5]), $segment5, 5, $dependencyList))
foreach($segments[6] as $segment6)
if(!testForExclusion(${'segment'.key($dependencyList[6])}, key($dependencyList[6]), $segment6, 6, $dependencyList))
foreach($segments[7] as $segment7)
if(!testForExclusion(${'segment'.key($dependencyList[7])}, key($dependencyList[7]), $segment7, 7, $dependencyList))
$partNumbers[] = $segment0.'-'.$segment1.'-'.$segment2.'-'.$segment3.'-'.$segment4.'-'.$segment5.'-'.$segment6.'-'.$segment7;
echo count( $partNumbers );
echo '<p>';
echo implode('<br/>', $partNumbers);
function testForExclusion($determiningValue, $determiningSegment, $testValue, $testSegment, $exclusionList){
$excluded = false;
if(isset($exclusionList[$testSegment][$determiningSegment][$determiningValue])){
if(!in_array($testValue, $exclusionList[$testSegment][$determiningSegment][$determiningValue])){
$excluded = true;
}
}
return $excluded;
}
EDIT: The data structure and dependency function should be able to be used for any combination of dependencies, so this solution should be expandable.
First I think it would be good to see if you can generate one part number by using array_rand() which picks one or more random entries out of an array. Later you can modify the code to use recursion or nested foreach to output all possible parts.
foreach ($parts as $part)
{
if (!is_array($part[0]))
$list[] = array_rand($part);
else
{
$val = "UNKNOWN";
// Handle dependencies by looking back to the relevant section of
// the current part number that is being generated.
foreach ($part as $bit)
if ($list[$bit['dependIndex']] == $bit['dependVal'])
$val = array_rand($bit['values']);
$list[] = $val;
}
}
var_dump($list);
Generating all possible parts is a fairly hard problem. If this is a homework question, your professor must have a lot of confidence in you!
There are 2 functions you are looking to perform. One for resolving the dependencies, and one for the number generation.
Given the dependencies in the following model:
$dependencies = array(
'300'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('A0','A1','A2')
),
'400'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('B0','B1','B2','C0')
),
'600'=>array(
0=>array('1M7','3M5'),
1=>array('D0')
),
'700'=>array(
0=>array('200','250','500','1M0','1M7','3M5'),
1=>array('E0')
),
'800'=>array(
0=>array('075','150','200','250','500','1M0','1M7','3M5'),
1=>array('F0')
),
'900'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('G0','G2','G3','G4')
),
'1G4'=>array(
0=>array('075','150','250','500','1M0','1M7','3M5','7M0'),
1=>array('H0','H1')
),
'1G8'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('H0','H1')
),
'2G0'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('I0')
),
'2G5'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('J0','J1')
)
);
you can create a function that combines positions 3, 4, and 5 into a pre-calculated string:
function position3($arrays){
$retVal = array();
foreach ($arrays as $key=>$value){
foreach ($value[0] as $v1){
foreach ($value[1] as $v2){
array_push($retVal, $key . '-' . $v1 . '-' . $v2);
}
}
}
return $retVal;
}
With position 3 combined in all of the defined mutations, you can map the positions into an array model as follows:
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = position3($dependencies);
$parts[3] = array('12','1L','24','48','AC');
$parts[4] = array('ET','FC','IC','FI','NT');
$parts[5] = array('OO');
You can then call a looping function that build each permutation of your combined values (in total, 13275) with this function:
function go($parts){
$total = 0;
foreach ($parts[0] as $pos1){
foreach ($parts[1] as $pos2){
foreach ($parts[2] as $pos3){
foreach ($parts[3] as $pos4){
foreach ($parts[4] as $pos5){
foreach ($parts[5] as $pos6 ){
echo implode('-', [$pos1,$pos2,$pos3,$pos4,$pos5,$pos6 ])."\n";
$total++;
}
}
}
}
}
}
echo $total;
}
Putting this all together will make:
function position3($arrays){
$retVal = array();
foreach ($arrays as $key=>$value){
foreach ($value[0] as $v1){
foreach ($value[1] as $v2){
#echo $key . '-' . $v1 . '-' . $v2 . "\n";
array_push($retVal, $key . '-' . $v1 . '-' . $v2);
}
}
}
return $retVal;
}
$dependencies = array(
'300'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('A0','A1','A2')
),
'400'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('B0','B1','B2','C0')
),
'600'=>array(
0=>array('1M7','3M5'),
1=>array('D0')
),
'700'=>array(
0=>array('200','250','500','1M0','1M7','3M5'),
1=>array('E0')
),
'800'=>array(
0=>array('075','150','200','250','500','1M0','1M7','3M5'),
1=>array('F0')
),
'900'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('G0','G2','G3','G4')
),
'1G4'=>array(
0=>array('075','150','250','500','1M0','1M7','3M5','7M0'),
1=>array('H0','H1')
),
'1G8'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('H0','H1')
),
'2G0'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('I0')
),
'2G5'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('J0','J1')
)
);
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = position3($dependencies);
$parts[3] = array('12','1L','24','48','AC');
$parts[4] = array('ET','FC','IC','FI','NT');
$parts[5] = array('OO');
function go($parts){
$total = 0;
foreach ($parts[0] as $pos1){
foreach ($parts[1] as $pos2){
foreach ($parts[2] as $pos3){
foreach ($parts[3] as $pos4){
foreach ($parts[4] as $pos5){
foreach ($parts[5] as $pos6 ){
echo implode('-', [$pos1,$pos2,$pos3,$pos4,$pos5,$pos6 ])."\n";
$total++;
}
}
}
}
}
}
echo $total;
}
go($parts);
I think I understand what you want here... this is a very brute-force way of dealing with it, but should work:
<?php
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$parts[3] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('075','150','250','500','1M0','1M7','3M5','7M0')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('250','500','1M0','1M7','3M5','7M0','14M'))
);
$parts[4] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('A0','A1','A2')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('B0','B1','B2','C0')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('D0')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('E0')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('F0')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('G0','G2','G3','G4')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('H0','H1')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('K0')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('I0')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('J0','J1'))
);
$parts[5] = array('12','1L','24','48','AC');
$parts[6] = array('ET','FC','IC','FI','NT');
$parts[7] = array('OO');
foreach($parts[1] as $eigth){
foreach($parts[2] as $sect){
foreach($parts[3] as $keya => $firstloop){
foreach($parts[3][$keya] as $itemone){
foreach($parts[3][$keya]['values'] as $firstselector){
foreach($parts[4] as $keyb => $secondloop){
foreach($parts[4][$keyb] as $itemtwo){
foreach($parts[4][$keyb]['values'] as $secondselector){
foreach($parts[5] as $halve){
foreach($parts[6] as $quarter){
if($sect == $parts[3][$keya]['dependVal'] && $sect == $parts[3][$keyb]['dependVal']){
echo $parts[0][0] . "-" . $eigth . "-" . $sect . "-" . $firstselector . "-" . $secondselector . "-" . $halve . "-" . $quarter . "<br>";
}
}
}
}
}
}
}
}
}
}
}
?>

Formatting url strings PHP array

Hi all I am sorry if this is a dumb question and I understand I might get banned for asking it but after a lot of work reading over PHP manual, reading the relevant chapters in PHP5.3 and scowering across Stackoverflow I am stuck in my tracks.
I have been universally format the url's taken in from a Search API I have tried to use parse_url(), trim and others unsuccessfully I decided upon str_replace
foreach ($jsonObj->RESULT as $value) {
$BLekko_results[] = array(
'url' => strip_tags($value->url),
'url' => str_replace("http://www.", "http://", $value->url),
'url' => str_replace("https://www.", "http://", $value->url),
'url' => str_replace( " http://", "http://", $value->url),
'url' => str_replace( " http://", "http://", $value->url),
title' => $value->url_title,); }
I plead humbly for you help ...
foreach ($jsonObj->RESULT as $value) {
$url = trim($value->url);
$find = array("http://www.", "https://www.", "https://");
$BLekko_results[] = array(
'url' => str_replace($find, "http://", $url),
'title' => $value->url_title,);
}
Perhaps try something like this:
public function processURLString($urlString) {
$urlString = trim($urlString);
if($urlString) {
$urlString = preg_replace('/https?:\/\//', '', $urlString);
$urlString = trim($urlString);
$urlString = 'http://'.$urlString;
}
return $urlString;
}
And then you can add or remove www etc...
$BLekko_results = array();
foreach ($jsonObj->RESULT as $value) {
$value = strip_tags($value->url);
$updatedURL = str_replace(array('http://www.','https://www.','https://'),"http://",$value->url);
$updatedTitle = $value->url_title;
$BLekko_results[] = array('url'=>$updatedURL,'title'=>$updatedTitle);
}
echo "<pre>";
print_r($BLekko_results);
echo "</pre>";

Categories