Separating values of an array in PHP - php

I am currently working on clean URL and stuck with this issue.
URL: http://localhost:8080/Pumps/Piston-pumps
$request_path = explode('?', $_SERVER['REQUEST_URI']);
$path_info = array_values($request_path);
echo str_replace('/',' ',$request_path[0]);
print_r($path_info);
above code gives below output :
Pumps Piston-pumps
Array ( [0] => /Pumps/Piston-pumps )
Is there a way I can convert the above array into:
Array ( [0] => Pumps [1] => piston-pumps )
I need this to add onto my URL
Thanks in advance

Here it is
<?php
$getUrl = "http://localhost:8080/Pumps/Piston-pumps"; // in your case it is $_SERVER['REQUEST_URI'];
$request_path = explode('?', str_ireplace(array("http://","https://"),"",$getUrl));
$expSlashes = explode("/", $request_path[0]);
$resultArr = array_slice($expSlashes, 1, count($expSlashes));
print_r($resultArr);
?>
use of explode and array_slice would result you with your required output.
explode is used to split a string with a needle sub-string
array_slice is used to create another array with limited and required number of array content

Try following code.
$new_array = ();
foreach($array as $k=>$v){
//This u split by upper case
$g = preg_split('/(?=[A-Z])/', $v);
$new_array[] = $g[0];
$new_array[] = strtolower($g[1]);
}

Use explode http://php.net/manual/en/function.explode.php
explode('/', '/Pumps/Piston-pumps');

Related

how to concat (_1) in string which is comma separeted in php?

this code is in php
$v = 1,2,3,4,5;
as I have to concat _1 in above variable
as I need this output $v = 1_1,2_1,3_1,4_1,5_1
Please refer to the PHP Manual:
implode — Join array elements with a string
explode — Split a string by string
In your case:
$v = "1,2,3,4,5";
echo implode("_1,", explode(",", $v)) . "_1";
On a side note: since your string is a comma separated value, you might also be interested in
str_getcsv — Parse a CSV string into an array
Without exploding/imploding, you can:
echo str_replace(',', '_1,', '1,2,3,4,5') . '_1';
$v = "1,2,3,4,5;";
$newValue = str_replace(",","_1,",$v); //replace , with _1,
$newValue = str_replace(";","_1;",$newValue); //replace ; with _1;
output:
1_1,2_1,3_1,4_1,5_1;
Use array map
$v = '1,2,3,4,5';
$arr = explode(',',$v);
$arr = array_map(function ($val){
return $val.'_1';
},$arr);
echo implode(',',$arr);
demo
I think you should put those numbers in quote.
$v = '1,2,3,4,5';
$new_v = explode(',', $v);
foreach ($new_v as $x) {
$v1[] = $x.'_1';
}
print_r($v1);
It will return array like this.
Array ( [0] => 1_1 [1] => 2_1 [2] => 3_1 [3] => 4_1 [4] => 5_1 )

How to split evenly and oddly a string to form an array of even and odd results OK Like

I have a php string formed by images and corresponding prices like OK Like
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
I know that if I do:
$myArray = explode(',', $myString);
print_r($myArray);
I will get :
Array
(
[0] => ddb94-b_mgr3043.jpg
[1] => 3800
[2] => 83acc-b_mgr3059.jpg
[3] => 4100
)
But How could I split the string so I can have an associative array of the form?
Array
(
"ddb94-b_mgr3043.jpg" => "3800"
"83acc-b_mgr3059.jpg" => "4100"
)
Easier way to do like below:-
<?php
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$chunks = array_chunk(explode(',', $myString), 2); //chunk array into 2-2 combination
$final_array = array();
foreach($chunks as $chunk){ //iterate over array
$final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair
}
print_r($final_array); //print final array
Output:-https://eval.in/859757
Here is another approach to achieve this,
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100,test.jpg,12321";
$arr = explode(",",$myString);
$temp = [];
array_walk($arr, function($item,$i) use (&$temp,$arr){
if($i % 2 != 0) // checking for odd values
$temp[$arr[$i-1]] = $item; // key will be even values
});
print_r($temp);
array_walk - Apply a user supplied function to every member of an array
Here is your working demo.
Try this Code... If you will receive all the key and value is equal it will work...
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$myArray = explode(',', $myString);
$how_many = count($myArray)/2;
for($i = 0; $i <= $how_many; $i = $i + 2){
$key = $myArray[$i];
$value = $myArray[$i+1];
// store it here
$arra[$key] = $value;
}
print_r($arra);

Textarea lines to array using php

I have a php variable that contain value of textarea as below.
Name:Jay
Email:jayviru#demo.com
Contact:9876541230
Now I want this lines to in array as below.
Array
(
[Name] =>Jay
[Email] =>jayviru#demo.com
[Contact] =>9876541230
)
I tried below,but won't worked:-
$test=explode("<br />", $text);
print_r($test);
you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it
<?php
$text = 'Name:Jay
Email:jayviru#demo.com
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);
$final_data = array();
foreach ($array_data as $data){
$format_data = explode(':',$data);
$final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);
and output is :
Array
(
[Name] => Jay
[Email] => jayviru#demo.com
[Contact] => 9876541230
)
Easiest way to do :-
$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);
$final_array = array();
foreach($textarea_array as $textarea_arr){
$exploded_array = explode(':',$textarea_arr);
$final_array[trim($exploded_array[0])] = trim($exploded_array[1]);
}
print_r($final_array);
Output:- https://eval.in/846556
This also works for me.
$convert_to_array = explode('<br/>', $my_string);
for($i=0; $i < count($convert_to_array ); $i++)
{
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array); ?>
I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array

Put string into key value array

Hi I got the following string:
info:infotext,
dimensions:dimensionstext
and i need to put these values into an array in PHP. What is the regex function to put these into an array. I studied the regex codes but it's kinds confusing to me.
I want to put the info as the key and he infotext as the value into an array like this:
Array {
[info] => infotext
[dimensions] => dimensionstext
}
Demo here
<?php
$string ='info:infotext,
dimensions:dimensionstext';
$array = array_map(function($v){return explode(':', trim($v));}, explode(',', $string));
foreach($array as $v)
{
$o[$v[0]] = $v[1];
}
print_r($o);
You can use array_chunk and array_combine
<?php
$input = 'info:infotext,
dimensions:dimensionstext';
$chunks = array_chunk(preg_split('/(:|,)/', $input), 2);
$result = array_combine(array_column($chunks, 0), array_column($chunks, 1));
print_r($result);
http://ideone.com/dRtref

Filter a string in PHP

I'm looking to get an array of ID's from the following string.
[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]
Ideally, i'd like to look at this string and get an array of the INT values within images. e.g.
array("3057", "2141", "234");
find images value and explode it to receive array
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
if (preg_match('/images\s*=\s*\"([^\"]+)\"/', $str, $m)) {
$res = explode(',', $m[1]);
print_r($res);
}
Another solution using explode and strpos functions:
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
foreach (explode(" ", $str) as $v) {
if (strpos($v, "images=") === 0) {
$result = explode(",", explode('"', $v)[1]);
break; // avoids redundant iterations
}
}
print_r($result);
The output:
Array
(
[0] => 3057
[1] => 2141
[2] => 234
)

Categories