extracting part of a string in php - php

These are my possible strings.
$str = 'price-100-500';
$str = 'price-200-600';
I want two variables from the string.
From first string, I want
$val1 = 100;
$val2 = 500;
From second string, I want
$val1 = 200;
$val2 = 600;
Please, how can I get?

Use PHP explode function. See my codes as below:
$str = 'price-100-500';
$arr = explode('-',$str);
$val1 = $arr[1];
$val2 = $arr[2];

Just do in simple way:
$str = 'price-100-500';
list($val1, $val2) = explode('-', str_replace('price-', '', $str));
echo '<br>val1 : '.$val1;
echo '<br>val2 : '.$val2;
Output
val1 : 100
val2 : 500

Use
$str = 'price-100-500';
$exploded=explode("-",$str);
echo $exploded[1];//will echo 100
echo $exploded[2];//will echo 500
Similarly you can explode any string

Assuming that you are willing to have this from several similar $str, and if you have them as an array, you can use the following code block to get them all as array output --
foreach($strings as $string){
$arr = explode('-',$string);
$result[$string]['val1'] = $arr[1];
$result[$string]['val2'] = $arr[2];
}
print_r($result);
This will give you an output like
Array
(
[price-100-500] => Array
(
[val1] => 100
[val2] => 500
)
[price-200-600] => Array
(
[val1] => 200
[val2] => 600
)
)
For better and exact answer you should provide better explanation of your problem. Hope for the best.
Thanks...:)

For your first example:
<?php
$str = 'price-100-500';
$arrayStr=explode("-",$str);
$val1 = $arrayStr[1];
$val2 = $arrayStr[2];
?>
If u wan't a function you can use this:
<?php
$str = 'price-100-500';
$array = getMyVar($str);
echo $array[0];
echo $array[1];
function getMyVar($str){
$arrayStr=explode("-",$str);
$val1 = $arrayStr[1];
$val2 = $arrayStr[2];
return array($val1,$val2);
}
?>

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);

Concatenate two different variable in PHP

I have the following code
<?php
$str="3dollars";
$a=20;
$a+=$str;
print($a);
?>
How can be the Output of the above program is 23 ?
Thanks In Advance !!
The + operator will coerce the string into being an integer, so it will (internally) do something like this:
$str = "3dollars";
$a = 20;
$a += $str;
// $str = (int)"3dollars";
// $str = 3;
$a = 23;
What you want to do is use the 'concatenation' operator (.):
<?php
$str = "3dollars";
$a = 20;
$a .= $str;
print($a); // 203dollars
It's taking "3dollars" as a number, getting $str = 3.
And when you echo, you add 20, to $str, so it prints 23 and $a = 23.
<?php $a += $str;
print($a);
it echo 23; //$a=$a+$b;?>
Use this
<?php
$a = 20;
$str = "3dollars";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
$matches = $a + $matches[0][0];
echo '<br> Value Is :'.$matches;
?>
This will print 23 as Answer.
Output
Array ( [0] => Array ( [0] => 3 ) )
Value Is :23
phpfiddle Preview
$str= (int) "3dollars";
$a=20;
$a+=$str;
print($a);
Use (int) to convert string to integer.

Each value of a string to a separate array conversion

I have a html page contents, that I converted into a string separated by "#".
Example:
(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6
Is there any way to convert each value of these string to convert into a array?
I need an output like this:
$a = (2R)-2-hydroxy
$b = 250.181
$c = C15H24NO2
$d = 2
$e = 1
//etc...
This should work for you:
Just explode() your string and loop through the array. Then you can assign each value to a variable, where you can increment the character.
$str = "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6";
$arr = explode("#", $str);
$start = "a";
foreach($arr as $v) {
$$start = $v;
$start++;
}
This should work
$array = explode("#", "(2R)-2-hydroxy#250.181#C15H24NO2#2#1#46#1#11#1.1266#1#18#6");
$array [0] = (2R)-2-hydroxy
$array [1] = 250.181
...

extract and sum two "slash" separated numbers e.g (50/20 = 70) in php?

I have this two digit number i.e. 50/20 which is seperated by slash and stored in one column of database.
$value = '50/20';
I want to get separate number as
$num1 = 50;
$num2 = 20;
and sum as
$sum = $num1+$num2;
Is there any solution for to separate those combine numbers.
Use explode
Try like this
$value = '50/20';
$arr = explode('/',$value);
$sum = $arr[0]+$arr[1];
//Output
$arr[0] contains 50
$arr[1] contains 20
You can check this by simply doing print_r($arr);
You can do also something like:
$value = '50/20';
$sum = array_sum(explode('/', $value));
echo $sum; // 70
$exp = explode('/',$value);
$value1 = $exp[0];
$value2 = $exp[1];
$sum = $value1 + $value2;
$value = '50/20';
$arr = explode('/',$value);
$num1=$arr[0];
$num2=$arr[1];
$sum=$num1+$num2;
print_r($sum);
Output
70
Explode Function is used to convert a sting to array.
This should work for you.
$value = "50/20";
$numbers = preg_split("/\//",$value);
$sum = $numbers[0]+$numbers[1];

Categories