I'm looking for a way to re-sort a string/array using index value.
Example: string (1,2,3,4,5) change to (1,5,2,3,4)
$string = "1,a,v,v|2,b,v,v|3,c,v,v";
// Convert string to array first...
$sections = explode ('|', $string);
$current_index = "1";
$new_index = "2";
$arrReorder = array();
foreach($sections as $key => $val){
if ($key = $current_index) {
$arrReorder[$new_index] = $val;
}
else {
$arrReorder[$key] = $val;
}
}
$new_string = implode("|", $arrReorder);
echo $new_string;
$new_string should output "1,a,v,v|3,c,v,v|2,b,v,v"
I found a solution for anyone trying to do the same thing:
$string = "1,2,3";
// Convert string to array first...
$sections = explode ('|', $string);
function array_move(&$sections, $oldpos, $newpos) {
if ($oldpos==$newpos) {return;}
array_splice($sections,max($newpos,0),0,array_splice($sections,max($oldpos,0),1));
}
array_move($sections, 0, 2);
print_r($sections);
Related
I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .
$string = "width,100;height,8600;block,700;narrow,1;"
I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"
below is my code php which i tried.Please advice me
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
for ($i = 0; $i < count($arr); $i++) {
if (in_array($arr, 'block')) {
unset($arr[$i]);
}
}
Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice
You essentially want to replace part of your string:
$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Try this:
$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
unset($arr['block']);
}
How to get this
function render($string, $array)
{
$pattern = '/[^{{=]*[\w][}}$]/';
preg_match_all($pattern, $string, $matches);
foreach($matches as $tt)
{
$index = '';
foreach($tt as $match1){
$match = str_replace('}', '', $match1);
if (strpos($match,'.') !== false)
{
$string_parts = explode('.', $match);
//print_r($string_parts);
foreach($string_parts as $part)
{
$index .="['".$part."']";
}
}
else
{
$index ="['".$match."']";
}
//echo '$array'.$index;
$new_str = str_replace("{{=".$match."}}", '{$array'.$index.'}' , $string);
$index = '';
//echo $new_str;
$string = $new_str;
}
}
return $string;
}
$arr = [
'site'=>'smartprix',
'users'=>[
['name'=>'user1', 'contact'=>'1234'],
['name'=>'user2', 'contect'=>'4321']
],
'location'=>['address'=>['pincode'=>'123456', 'city'=>'Noida'],
'near'=>'mamura']
];
$array = $arr;
//echo "{$array['site']}"; //It is printing smartprix
$string = "{{=site}} is located at pincode {{=location.address.pincode}}";
echo render($string, $array);
// it is printing "{$array['site']} is located at pincode {$array['location']['address']['pincode']}" why it not convert $array['site'] into the value. I read on php manual and got some reference that {} do not work on returned string then what is the method so that i can print array values after returning the string ?
You can print your expected string is.
echo $string = $array['site']." is located at pincode ".$arr['location']['address']['pincode'];
I'm having trouble using preg_match_all to split a string into key value pairs. An example of my string:
"%Title:Movie%Sortable%Writer:%Indexed:false%"
Where I expect results like:
$result['Title'] = 'Movie';
$result['Sortable'] = '';
$result['Writer'] = '';
$result['Indexed'] = 'false';
I can split the string using preg_match('/%/',$str,-1,PREG_SPLIT_NO_EMPTY); but it returns an indexed array. I need an associative array so that order is not important and I can use the key in a switch statement. What would be the correct regex to use in preg_match_all?
Try with:
$input = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$output = array();
$data = explode('%', $input);
foreach ($data as $item) {
list($key, $value) = explode(':', $item);
$output[$key] = $value;
}
<?php
$arr = array();
$string = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$d = explode('%', $string);
foreach($d as $item){
list($key,$value) = explode(':', $item);
$arr[$key] = $value;
}
print_r($arr);
?>
I got this
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>'
I want to remove only what comes after <p>text3</p>
My result would be:
$description = '<p>text1</p><p>text2</p><p>text3</p>'
My guess is that we need to use preg_replace with some regex but I can't manage to write a working one.
You could...
function str_occurance($needle, $haystack, $occurance) {
$occurance += 2;
$arr = explode($needle, $haystack, $occurance);
unset($arr[0]);
$arr = array_values($arr);
$key = count($arr) - 1;
unset($arr[$key]);
$str = $needle . implode($needle, $arr);
return $str;
}
Not the prettiest, but it works.
Edit: To use:
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>';
$split = '<p>';
$return = 3;
$new = str_occurance($needle, $description, $return);
echo $new; // returns <p>text1</p><p>text2</p><p>text3</p>
I have a string containing ; subdivided by , and would like to make an echo of a chosen value.
My string is $string='Width,10;Height,5;Size,1,2,3'
I want to make an echo of the Height value (echo result must be 5)
$parts = explode(';', $string);
$component = explode(',', $parts[1]); // [1] is the Height,5 portion
echo $component[1]; // 5
Or this:
$p = explode(';', $string);
$data = array();
foreach($p as $part) {
$split = explode(',',$part,2); //the 'Size' bit different that the rest. I assume 1,2,3 is the value for Size?
$data[$split[0]] = $split[1];
}
$what_you_want_to_find = 'Height';
echo $data[$what_you_want_to_find];
try this:
$attrs = explode(";", $string);
$attrHeight = "";
foreach ($attrs as $value) {
if (strpos($value, "Height") !== false)
$attrHeight = explode(",", $value);
}
echo $attrHeight;