PHP explode and array index - php

How can I get the following code to work?
$a = explode('s', $str)[0];
I only see solutions looking like this:
$a = explode('s', $str); $a=$a[0];

As others have said, PHP is unlike JavaScript in that it can't access array elements from function returns.
The second method you listed works. You can also grab the first element of the array with the current(), reset(), or array_pop() functions like so:
$a = current( explode( 's', $str ) ); //or
$a = reset( explode( 's', $str ) ); //or
$a = array_pop ( explode( 's', $str ) );
If you would like to remove the slight overhead that explode may cause due to multiple separations, you can set its limit to 2 by passing two after the other arguments. You may also consider using str_pos and strstr instead:
$a = substr( $str, 0, strpos( $str, 's' ) );
Any of these choices will work.
EDIT Another way would be to use list() (see PHP doc). With it you can grab any element:
list( $first ) = explode( 's', $str ); //First
list( ,$second ) = explode( 's', $str ); //Second
list( ,,$third ) = explode( 's', $str ); //Third
//etc.
That not your style? You can always write a small helper function to grab elements from functions that return arrays:
function array_grab( $arr, $key ) { return( $arr[$key] ); }
$part = array_grab( explode( 's', $str ), 0 ); //Usage: 1st element, etc.
EDIT: PHP 5.4 will support array dereferencing, so you will be able to do:
$first_element = explode(',','A,B,C')[0];

You are correct with your second code-block. explode, and other functions can't return a fully formed array for immediate use,and so you have to set a temporary variable. There may be code in the development tree to do that, but the only way to get the elements you need for now, is the temporary variable.

use current
$a = current(explode('s', $str));
but I found is ugly

You can use this:
$a = array_shift(array_slice(explode("s", $str), 0, 1)));
This is the best way I have found to get a specific element from an array from explode.
Breakdown:
Explode returns an array on delimiter
array_slice($arrayname, $offset, $length) gives you a new array with all items from offset, lenght
array_shift($array) gives you the first (and in this case, the only) item in the array passed to it.
This doesen't look pretty, but does the same as:
$a = explode('s', $str)[0];
There must be a better way to do this, but I have not found it.
Update:
I was investigating this because I wanted to extract a portion of a URL, so i did the following tests:
function urlsplitTest()
{
$url = 'control_panel/deliveryaddress/188/edit/';
$passes = 1000000;
Timer::reset();
Timer::start();
$x =0;
while ($x<$passes) {
$res = array_shift(array_slice(explode("/", $url), 2, 1));
$x++;
}
Timer::stop();
$time = Timer::get();
echo $res.'<br />Time used on: array_shift(array_slice(explode("/", $url), 2, 1)):'.$time;
Timer::reset();
Timer::start();
$x =0;
while ($x<$passes) {
$res = array_get(explode("/", $url), 2);
$x++;
}
Timer::stop();
$time = Timer::get();
echo $res.'<br />Time used on: array_get(explode("/", $url), 2): '.$time;
Timer::reset();
Timer::start();
$x =0;
while ($x<$passes) {
$res = substr($url, 30, -6);
$x++;
}
Timer::stop();
$time = Timer::get();
echo $res.'<br />Time used on: substr($url, 30, -6): '.$time;
}
function array_get($array, $pos) {return $array[$pos];}
The results were as following:
Time used on: array_shift(array_slice(explode("/", $url), 2, 1)):7.897379
Time used on: array_get(explode("/", $url), 2): 2.979483
Time used on: substr($url, 30, -6): 0.932806
In my case i wanted to get the number 188 from the url, and all the rest of the url was static, so i ended up using the substr method, but for a dynamic version where lenth may change, the array_get method above is the fastets and cleanest.

Related

PHP remove values below a given value in a "|"-separated string

I have this value:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389"
And I want to remove the values below 800130 including the starting "|". I guess it is possible, but I can not find any examples anywhere. If anyone can point me to the right direction I would be thankful.
You could split the input string on pipe, then remove all array elements which, when cast to numbers, are less than 800130. Then, recombine to a pipe delimited string.
$input= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$input = ltrim($input, '|');
$numbers = explode("|", $input);
$array = [];
foreach ($numbers as $number) {
if ($number >= 800130) array_push($array, $number);
}
$output = implode("|", $array);
echo "|" . $output;
This prints:
|800134|800215|800317|800341|800389
This should work as well:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
function my_filter($value) {
return ($value >= "800130");
}
$x = explode("|", $numbers); // Convert to array
$y = array_filter($x, "my_filter"); // Filter out elements
$z = implode("|", $y); // Convert to string again
echo $z;
Note that it's not necessary to have different variables (x,y,z). It's just there to make it a little bit easier to follow the code :)
PHP has a built in function preg_replace_callback which takes a regular expression - in your case \|(\d+) - and applies a callback function to the matched values. Which means you can do this with a simple comparison of each matched value...
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
echo preg_replace_callback("/\|(\d+)/", function($match){
return $match[1] < 800130 ? "" : $match[0];
}, $numbers);
Use explode and implode functions and delete the values that are less than 80031:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$values = explode("|", $numbers);
for ($i=1;$i<sizeof($values);$i++) {
if (intval($values[$i])<800130) {
unset($values[$i]);
}
}
// Notice I didn't start the $i index from 0 in the for loop above because the string is starting with "|", the first index value for explode is ""
// If you will not do this, you will get "|" in the end in the resulting string, instead of start.
$result = implode("|", $values);
echo $result;
It will print:
|800134|800215|800317|800341|800389
You can split them with a regex and then filter the array.
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$below = '|'.join('|', array_filter(preg_split('/\|/', $numbers, -1, PREG_SPLIT_NO_EMPTY), fn($n) => $n < 800130));
|800027|800036|800079|800097

Pad all dot-delimited substrings to the same length for all strings in array

I have this array:
$versions = [
'1',
'1.0.2.4',
'1.1.0',
'1.12.547.8'
];
And I want:
$versions = [
'001',
'001.000.002.004',
'001.001.000',
'001.012.547.008'
];
But, if I have this:
$versions = [
'1',
'1.12'
];
I want this:
$versions = [
'01',
'01.12'
];
In other words, each string chunk must have same len of max chunk length considering every chunk throughout the array.
Explanation:
I have an array with variable elements number. Each element is a string containing a variable amount of groups of digits -- each separated by a dot. Each digit group has a variable length. I want to pad each sequence of digits so that all array sub-groups have the same length as the max group length.
In the first example, max length is 3 (longest number: 547), so all groups of final array have 3 digits (001, 000, ...). In the second example, max length is 2 (12), so all groups have 2 digits (01, 01, 12).
My solution:
$max = max(
array_map(
'strlen',
call_user_func_array(
'array_merge',
array_map(
function($row) {
return explode( '.', $row );
},
$versions
)
)
)
);
foreach($versions as &$version)
{
$version = preg_replace(
'/\d*(\d{' . $max . '})/',
'\1',
preg_replace(
'/(\d+)/',
str_repeat('0', $max) . '\1',
$version
)
);
}
Explained:
$tmp = array_map(
function($row) {
return explode('.', $row );
},
$versions
);
$tmp = call_user_func_array('array_merge', $tmp);
$tmp = array_map( 'strlen', $tmp );
$max = max($tmp);
$tmp = preg_replace('/(\d+)/', str_repeat('0', $max ) . '\1', $version );
$version = preg_replace( '/\d*(\d{' . $max . '})/', '\1', $tmp);
Someone have any ideas on how to simplify the process?
Well you can just simplify everything what you already did:
First we can implode() the array into a string and match all numbers with preg_match_all().
To then get the longest number we just use array_map() with strlen() as callback and get the longest number with max().
Then when you know your pad length you can go through your array with preg_replace_callback() and pad your numbers with str_pad().
Code:
<?php
$versions = [ '1', '1.0.2.4', '1.1.0', '1.12.547.8' ];
preg_match_all("/\d+/", implode(".", $versions), $m);
$max = max(array_map("strlen", $m[0]));
$new = preg_replace_callback("/(\d+)/", function($m)use($max){
return str_pad($m[1], $max, "0", STR_PAD_LEFT);
}, $versions);
print_r($new);
?>
My modern take on this task resembles #Rizier123's approach, but should be a little more efficient because it doesn't it doesn't use a preg_match_all() call or iterated strlen() calls to determine the padding length.
My snippet uses a different native function to pad each encountered numeric substring, but it otherwise iterates the array of delimited values in the same way that #Rizier123's snippet does. My preg_replace_callback() technique uses PHP7.4's arrow function syntax and the pattern does not bother using a capture group.
Code: (Demo)
$pad = strlen(
max(
explode(
'.',
implode('.', $versions)
)
)
);
var_export(
preg_replace_callback(
'/\d+/',
fn($m) => sprintf("%0{$pad}d", $m[0]),
$versions
)
);

Insert into php array after certain value?

I have a string like this,
sidePanel[]=1&sidePanel[]=2&sidePanel[]=4
And if I need to add another value to the string I do this:
$sidePanel = explode('&', $_SESSION['sidePanel']);
array_push($sidePanel, 'sidePanel[]=3');
$sidePanel = implode('&', $sidePanel);
Which returns this:
sidePanel[]=1&sidePanel[]=2&sidePanel[]=4&sidePanel[]3
Now how can I make it so that it will always insert after the array 'sidePanel[]=2' when I explode it at &?
I do not want it in numerical order although this example will return it in numerical order, as the string can change to be in any order.
I cannot use array_splice as I understand this uses the key of the array, and as the position of sidePanel[]=2 can change it will not always be the same.
You can indeed use array_splice, but you have to find the position of your insertion point first:
$sidePanelArr = explode( '&', $_SESSION['sidePanel'] );
// find the position of 'sidePanel[]=2' in array
$p = array_search( 'sidePanel[]=2', $sidePanelArr );
// insert after it
array_splice( $sidePanelArr, p+1, 0, 'sidePanel[]=3' );
$sidePanelSt = implode( '&', $sidePanelArr );
You could also splice the string right into your original string without exploding and re-imploding.
The function substr_replace() is your friend:
$sidePanelSt = $_SESSION['sidePanel'];
// find the position of 'sidePanel[]=2' in string
// (adding '&' to the search string makes sure that 'sidePanel[]=2' does not match 'sidePanel[]=22')
$p = strpos( $sidePanelSt.'&', 'sidePanel[]=2&') + strlen('sidePanel[]=2' );
// insert after it (incl. leading '&')
$sidePanelSt = substr_replace( $sidePanelSt , '&sidePanel[]=3' , $p, 0 );
See : http://codepad.org/5AOXcHPk
<?php
$str = "sidePanel[]=1&sidePanel[]=2&sidePanel[]=4";
$sidePanelArr = explode('&', $str);
$newVal = 'sidePanel[]=3';
$insertAt = 2 ;
$newArr = array_merge(array_slice($sidePanelArr, 0,$insertAt),
array($newVal),
array_slice($sidePanelArr,$insertAt)
);
$sidePanel = implode('&', $newArr);
echo $sidePanel,PHP_EOL ;
?>
You could turn it into an array using parse_str and locate the value you want to insert it after:
// Turn it into an array
$url = parse_str($_SESSION['sidePanel']));
// What value do we want to insert it after
$insert_after = 2;
// The value you want to insert
$sidePanel = 3;
// Find the position of $insert_after
$offset = array_search($insert_after, $url['sidePanel']);
// Slice the array up (based on the value)
$url['sidePanel'] = array_merge(array_slice($url['sidePanel'], 0, $offset),
array($sidePanel),
array_slice($url['sidePanel'], $offset));
// Turn it back into a string
echo http_build_query($url);

"Unfolding" a String

I have a set of strings, each string has a variable number of segments separated by pipes (|), e.g.:
$string = 'abc|b|ac';
Each segment with more than one char should be expanded into all the possible one char combinations, for 3 segments the following "algorithm" works wonderfully:
$result = array();
$string = explode('|', 'abc|b|ac');
foreach (str_split($string[0]) as $i)
{
foreach (str_split($string[1]) as $j)
{
foreach (str_split($string[2]) as $k)
{
$result[] = implode('|', array($i, $j, $k)); // more...
}
}
}
print_r($result);
Output:
$result = array('a|b|a', 'a|b|c', 'b|b|a', 'b|b|c', 'c|b|a', 'c|b|c');
Obviously, for more than 3 segments the code starts to get extremely messy, since I need to add (and check) more and more inner loops. I tried coming up with a dynamic solution but I can't figure out how to generate the correct combination for all the segments (individually and as a whole). I also looked at some combinatorics source code but I'm unable to combine the different combinations of my segments.
I appreciate if anyone can point me in the right direction.
Recursion to the rescue (you might need to tweak a bit to cover edge cases, but it works):
function explodinator($str) {
$segments = explode('|', $str);
$pieces = array_map('str_split', $segments);
return e_helper($pieces);
}
function e_helper($pieces) {
if (count($pieces) == 1)
return $pieces[0];
$first = array_shift($pieces);
$subs = e_helper($pieces);
foreach($first as $char) {
foreach ($subs as $sub) {
$result[] = $char . '|' . $sub;
}
}
return $result;
}
print_r(explodinator('abc|b|ac'));
Outputs:
Array
(
[0] => a|b|a
[1] => a|b|c
[2] => b|b|a
[3] => b|b|c
[4] => c|b|a
[5] => c|b|c
)
As seen on ideone.
This looks like a job for recursive programming! :P
I first looked at this and thought it was going to be a on-liner (and probably is in perl).
There are other non-recursive ways (enumerate all combinations of indexes into segments then loop through, for example) but I think this is more interesting, and probably 'better'.
$str = explode('|', 'abc|b|ac');
$strlen = count( $str );
$results = array();
function splitAndForeach( $bchar , $oldindex, $tempthread) {
global $strlen, $str, $results;
$temp = $tempthread;
$newindex = $oldindex + 1;
if ( $bchar != '') { array_push($temp, $bchar ); }
if ( $newindex <= $strlen ){
print "starting foreach loop on string '".$str[$newindex-1]."' \n";
foreach(str_split( $str[$newindex - 1] ) as $c) {
print "Going into next depth ($newindex) of recursion on char $c \n";
splitAndForeach( $c , $newindex, $temp);
}
} else {
$found = implode('|', $temp);
print "Array length (max recursion depth) reached, result: $found \n";
array_push( $results, $found );
$temp = $tempthread;
$index = 0;
print "***************** Reset index to 0 *****************\n\n";
}
}
splitAndForeach('', 0, array() );
print "your results: \n";
print_r($results);
You could have two arrays: the alternatives and a current counter.
$alternatives = array(array('a', 'b', 'c'), array('b'), array('a', 'c'));
$counter = array(0, 0, 0);
Then, in a loop, you increment the "last digit" of the counter, and if that is equal to the number of alternatives for that position, you reset that "digit" to zero and increment the "digit" left to it. This works just like counting with decimal numbers.
The string for each step is built by concatenating the $alternatives[$i][$counter[$i]] for each digit.
You are finished when the "first digit" becomes as large as the number of alternatives for that digit.
Example: for the above variables, the counter would get the following values in the steps:
0,0,0
0,0,1
1,0,0 (overflow in the last two digit)
1,0,1
2,0,0 (overflow in the last two digits)
2,0,1
3,0,0 (finished, since the first "digit" has only 3 alternatives)

isolating part of a url with php

If I have the following url:
http://URL/products/38/293/bannana_cake/
or
htp://URL/products/38/293/fruit_cake/
How can I isolate just bannana_cake and fruit_cake from the examples above?
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
And then use explode() on the 'path' element.
For example:
<?php
$url = 'http://URL/products/38/293/bannana%5Fcake/';
$a = parse_url($url);
$p = explode('/', $a['path']);
echo $p[4];
?>
Probably the easiest way, which only applies to your special case and is not something for production, is to use the basename-function:
<?php
echo basename("http://url/products/38/293/banana_cake/"); // Produces "banana_cake"
?>
This only works because "banana_cake" is the last part of the url and there is nothing behind the last slash.
It is definately not a desirable solution and Luca Matteis' answer will get my vote, because the slightest change in the query string order will break things.
it will be a $_GET variable as what you're looking at is just a mod_rewrite version of a query string
try this to see what the variable name is:
<pre>
<?php print_r($_GET);?>
</pre>
You can see the URL that has been requested by looking at the $_SERVER[] array (Google that to find the exact entry). They you can split the string into an array on '/', then the [3] index will be the part of the URL you're interested in.
Split the url on the slashes and retrieve the last part:
$parts = explode('/', $url);
echo $parts[sizeof($parts) - 2];
Only problem, you need to have the trailing slash in the url. You could make a check for that like this:
$parts = explode('/', $url);
echo ($parts[sizeof($parts) - 1])
? $parts[sizeof($parts) - 1]
: $parts[sizeof($parts) - 2];
My answer will be slightly longer. It looks like you want to do something similar to using URI Templates, so here's a snippet of two functions from a class (called xs_Breakdown) I have that does these things. It could easily be extended to include wildcards and conditional behaviour (on the todo list for a time in the future I'm suffering from too little to do). First, and example of setting up and use ;
$br = new xs_Breakdown ( '{first}/{second}/{third}/{fourth}/{fifth}/{andsoon}' ) ;
// Pick out the template variable called 'third'
$third = $br->third ;
The code (just the basics which should be enough to kick up some of your own dust; all the code would be too long to post here. Pop me a message if you'd like the whole shebang with three nested property / Java Bean-like classes) ;
// Static variable to hold our tokens
$_tokens = null ;
// Input path (set in constructor)
$_path = null ;
// Results here
$values = array() ;
function parse ( $schema = '' ) {
// Sanitize input data : Regular Expression
$regexp = '/[^a-z0-9 +\-\/!$*_=|.:]/i' ;
// Break our path into little bits
$break = explode ( '/', $this->_path ) ;
// Find the tokens used from our schema template
$this->_tokens = $this->getSubStrs ( "{","}", $schema ) ;
// Loop through the path elements
foreach ( $break as $key => $value ) {
// Sanitize the value of the element
$value = urldecode ( trim ( preg_replace ( $regexp, '', $value ) ) ) ;
// Element not blank? (Meaning, real text)
if ( $value != '' )
// Index it!
#$this->values[$this->_tokens[$key]] = $value ;
}
}
function getSubStrs ( $from, $to, $str, &$result = array () ) {
if ( strpos ( $str, $from ) !== false ) {
$start = strpos ( $str, $from ) + 1 ;
$end = strpos ( $str, $to ) - 1 ;
$item = substr ( $str, $start, $end - $start + 1 ) ;
$rest = substr ( $str, $end + 2 ) ;
$result[] = $item ;
$this->getSubStrs ( $from, $to, $rest, $result ) ;
}
return $result ;
}

Categories