Extract items from string and assign them to variables [duplicate] - php

This question already has answers here:
explode string into variables
(2 answers)
Closed 5 years ago.
I need to use php and extract the items from the string and assign them to variables.
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
How can I get:
$var1 = 76305203
$var2 = 124400884

To create variables use list()
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
list($var1,$var2) = explode(';', $string);
echo $var1;
echo PHP_EOL;
echo $var2;
Output:- https://eval.in/928536
Or use explode() to get array and use that array
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
$array = explode(';', $string);
echo $array[0];
echo PHP_EOL;
echo $array[1];
Output:-https://eval.in/928537

Related

How to add up numbers in a php string? [duplicate]

This question already has answers here:
how to do a sum on a string in php?
(5 answers)
Closed 8 months ago.
I have a PHP string that contains numbers. Is there a way to add up all the numbers in a string?
For example: I have a string called $soc_count. That string outputs the following:
1 1
Is there a way to add up all the numbers in a string?
My code:
<?php $soc_count = [];$soc_count = (get_row_layout() == 'irl_today_social_media');?>
<?php echo $soc_count;?>
Assuming numbers are positive integers and string is not empty, you can try this:
eval('$sum = ' . trim(preg_replace('/[^0-9]+/', "+0+", $soc_count), '+') . ';');
echo $sum;
Use explode on your string of numbers, and then sum up the array:
$soc_count = "1 1";
$nums = explode(" ", $soc_count);
$total = array_sum($nums);
echo $total;
For a more general solution, which would cover a string of any type having numbers in it, we can try using preg_match_all:
$soc_count = "1 quick brown fox jumped over 1 lazy dog.";
preg_match_all("/\d+/", $soc_count, $matches);
$nums = $matches[0];
$total = array_sum($nums);
echo $total;

Combine two strings with for loop [duplicate]

This question already has answers here:
concat a for loop - php
(3 answers)
Closed 2 years ago.
Is it possible to combine two strings with for loop?
For example:
echo 'Prefix '.for($i=0;$i<4;$i++){ echo $i; }.' suffix';
This is not possible:
echo 'Prefix ';
for($i=0;$i<4;$i++)
{
echo $i;
}
echo ' suffix';
Because I would like to save a page using file_put_contents and source has a combination of HTML and PHP.
I would like to get:
$page = <beginning_of_html_page_here>
<php_code_here>
<end_html_page_here>
file_put_contents(page.html, $page);
You can use string concatenation. Use the dot . to join to strings, 'a'.'b' will give 'ab'. And $a .= 'c' will append 'c' to the $a variable.
// Create the string
$string = 'Prefix ';
for($i=0;$i<4;$i++)
{
// Append the numbers to the string
$string .= $i;
}
// Append the suffix to the string
$string .= ' suffix';
// Display the string
echo $string;
Result is:
Prefix 0123 suffix
Demo at Codepad.
About the end of your question, you can use this logic:
$page = '<beginning_of_html_page_here>';
// Append things to your string with PHP
$page .= 'something'
$page .= '<end_html_page_here>';
About your first code block, this can also be done by using two functions: range() to generate an array of numbers and implode() to join the array's items:
<?php
// Create the string
$string = 'Prefix '.implode('', range(0, 3)).' suffix';
echo $string;

Correct way of combining variables? [duplicate]

This question already has answers here:
How to join strings in PHP?
(3 answers)
Closed 7 years ago.
I have the following -
function button_class_add() {
global $uno_dos;
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
?>
<script type="text/javascript">
jQuery(function($) {
$('.std-button').addClass('<?php echo $btn_shape_class $btn_size_class; ?>');
});
</script>
<?php }
add_action('wp_footer', 'button_class_add');
How can I correctly combine these 2 variables so I can just echo out the combined variable?
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
Many thanks
Combining these lines -
$btn_shape_class = $uno_dos['buttons-shape-select-general'];
$btn_size_class = $uno_dos['buttons-size-select-general'];
Into something like -
$btn_shape = $uno_dos['buttons-shape-select-general']$uno_dos['buttons-size-select-general'];
Then I can just echo the 1 variable.
You can just use the "." string operator:
http://php.net/manual/en/language.operators.string.php
$combined = $btn_shape_class.$btn_size_class
or if you want to have a whitespace between, like in your current code:
$combined = $btn_shape_class.' '.$btn_size_class
It looks like by "combine", you mean "concatenate". Concatenating allows you to append a string to the end of another string.
If you want to concatenate two variables with different values then you just use a fullstop as follows:
// $variable_1 = 'hot';
// $variable_2 = 'dog';
echo $variable_1 . $variable_2;
// 'hotdog'
If you want to add whitespace between the two variables, on the other hand, you would need to concatenate the whitespace as follows:
// $variable_1 = 'hot';
// $variable_2 = 'dog';
echo $variable_1 . ' ' . $variable_2;
// 'hot dog'

How to parse string to array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 8 months ago.
I have a string like this:
"birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s="
I want to parse it to array in php. How to do it ?. Thanks for your help so much.
You use parse_str for this. (http://php.net/manual/en/function.parse-str.php)
$output = array();
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Or you can parse it into local variables
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
You can try the following :
inside this foreach you will get all the get parameters and then you can store them in an array.
foreach($_GET as $key => $value) {
//your code here
}
use
parse_str( $string );
Refer: http://php.net/manual/en/function.parse-str.php
$your_str = 'birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s=';
$_VARS = array();
parse_str($your_str, $_VARS);
echo $_VARS['birs_appointment_price'];

PHP implode named array [duplicate]

This question already has answers here:
Imploding an associative array in PHP
(10 answers)
Closed 8 years ago.
I have a named array that look like this:
$arr = array('name'=>'somename','value'=>'somevalue');
I want to turn that array into something like this:
name='somename' value='somevalue'
I tried http_build_query() to do it.
echo http_build_query($arr, '', ' ');
But this is the result I get:
name=somename%21 value=somevalue%21
How can I get the result I want with http_build_query()? Or, is there any PHP function to do it?
Thank you.
http_build_query returns a urlencoded string. If you don't want that you can run it through urldecode
$arr = array('name'=>"'somename'",'value'=>"'somevalue'");
print urldecode(http_build_query($arr,null,' '));
Try with foreach()
$arr = array('name'=>'somename','value'=>'somevalue');
$str = '';
foreach($arr as $k=>$v) {
$str.= "$k='$v' ";
}
echo $str;
foreach ($array as $key => $value) {
$result .= "$key='$value' ";
}
echo rtrim($result,' ');

Categories