How to get sub String from String in Php - php

I have a string like
$totalValues ="4565:4,4566:5,4567:6";
Now i want only values after ':' with coma separated ,i.e i want string like $SubValues = "4,5,6"
Thanks

using array_map, implode and explode
$totalValues ="4565:4,4566:5,4567:6";
echo implode(',',array_map(function($val){
$val = explode(':',$val);
return $val[1];
},explode(',',$totalValues)));

try this code it is working
<?php
$str="4565:4,4566:5,4567:6";;
$data =explode(",", $str);
echo '<pre>';
print_r($data);
$newstring="";
foreach ($data as $key => $value) {
preg_match('/:(.*)/',$value,$matches);
$newstring.=$matches[1].",";
}
echo rtrim($newstring,",");
Output
i have updated my code please check it
<?php
$str="4565:4,4566:5,4567:6";
$data1=explode(",", $str);
$newstring1="";
foreach ($data1 as $key1 => $value) {
preg_match('/(.*?):/',$value,$matches);
$newstring1.=$matches[1].",";
}
echo rtrim($newstring1,",");

you might want it this way:
$totalValues ="4565:4,4566:5,4567:6";
$temp = explode(':', $totalValues);
$subValues = $temp[1][0];
$x = count($temp);
for ($i = 2; $i < $x; $i++) {
$subValues .= ',' . $temp[$i][0];
}
echo $subValues;

Related

PHP Merge with separators

I am trying to merge two strings in a specific way.
Essentially I need to merge the first two strings into a third with a pipe symbol between them then separated by commas:
$merge1 = "id1,id2,id3"
$merge2 = "data1,data2,data3"
Those two would become:
$merged = "id1|data1,id2|data2,id3|data3"
I hope this makes sense?
I mean there is no PHP function that can output what you need.
Code produced desired output could be
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merged = [];
$arr1 = explode(',', $merge1);
$arr2 = explode(',', $merge2);
foreach ($arr1 as $key => $val) {
$merged[] = $val . '|' . $arr2[$key];
}
echo implode(',', $merged);
// id1|data1,id2|data2,id3|data3
This script will help you
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",", $merge1);
$merge2 = explode(",", $merge2);
$final = [];
foreach ($merge1 as $index => $value) {
$final[] = $value . "|" . $merge2[$index];
}
$final = implode(",", $final);
print_r($final);
output
id1|data1,id2|data2,id3|data3
Try this.
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",",$merge1);
$merge2 = explode(",",$merge2);
$mergeArr = array_combine($merge1,$merge2);
$mergeStr = [];
foreach($mergeArr as $k => $v) {
$mergeStr[] = $k.'|'.$v;
}
$mergeStr = implode(",",$mergeStr);
echo $mergeStr;
?>

Split word from string using PHP

I tried to split the characters from the available string, but have not found the right way.
I have a string like below :
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
foreach ($k as $l) {
var_dump(substr_replace($tara, '', $i, count($l)));
}
The result I want is :
'wordpress',
'wordpress/corporate',
'wordpress/corporate/business'
You could concatenate in every iteration like :
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$result = "";
foreach ($k as $l) {
$result .= '/'.$l;
echo $result."<br>";
}
The output will be :
/wordpress
/wordpress/corporate
/wordpress/corporate/business
If you don't need the slashes at the start you could add a condition like :
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$result = "";
foreach ($k as $l)
{
if( empty($result) ){
echo $l."<br>";
$result = $l;
}else{
$result .= '/'.$l;
echo $result."<br>";
}
}
Or using the shortened version inside loop with ternary operation, like :
foreach ($k as $l)
{
$result .= empty($result) ? $l : '/'.$l;
echo $result."<br>";
}
The output will be :
wordpress
wordpress/corporate
wordpress/corporate/business
How about constructing the results from the array alements:
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$s = '';
foreach ($k as $l) {
$s .= ($s != '' ? '/' : '') . $l;
var_dump($s);
}
This results in:
string(9) "wordpress"
string(19) "wordpress/corporate"
string(28) "wordpress/corporate/business"
You should explode your string array and print in foreach all exploded values using implode and temporary array(for example). Here is working code:
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$tmp = [];
foreach ($k as $l) {
$tmp[] .= $l;
var_dump(implode($tmp, '/'));
}
Create new array and inside foreach
<?php
$tara = 'wordpress/corporate/business';
$url = array();
foreach(explode("/",$tara) as $val){
$url[] = $val;
echo implode("/",$url)."\n";
}
?>
Demo : https://eval.in/932527
Output is
wordpress
wordpress/corporate
wordpress/corporate/business
Try This, Its Easiest :
$tara = 'wordpress/corporate/business';
$k = explode('/', $tara);
$str = array();
foreach ($k as $l) {
$str[] = $l;
echo implode('/',$str)."<br>";
}

How to splitt a string to an array with keys and values using php

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']);
}

Output in an A-Z and non A-Z characters in a List

I am trying to output any array to a directory list format.
A-Z is working, but I want to output words that don't begin with A-Z to the symbol #.
E.G. 1234, #qwerty, !qwerty, etc should be sorted to the # group.
<?php
$aTest = array('apple', 'pineapple', 'banana', 'kiwi', 'pear', 'strawberry', '1234', '#qwerty', '!qwerty');
$range = range('A','Z');
$range[] = "#";
$output = array();
foreach($range AS $letters){
foreach($aTest AS $fruit){
if(ucfirst($fruit[0]) == $letters){
$output[$letters][] = ucfirst($fruit);
}
}
}
foreach($output AS $letter => $fruits){
echo $letter . "<br/>--------<br/>\n";
sort($fruits);
foreach($fruits AS $indFruit){
echo $indFruit . "<br/>\n";
}
echo "<br/>\n";
}
?>
$output['#'] = array();
foreach($range as $letter){
$output[$letter] = array();
}
foreach($aTest AS $fruit){
$uc = ucfirst($fruit);
if(array_search($uc[0], $range) === FALSE){
$output['#'][] = $uc;
} else {
$output[$uc[0]][] = $uc;
}
}
notice that I have removed outer loop, as you don't need it
You should reverse the order of the two foreach loops, use break and a temporary variable:
foreach($aTest as $fruit){
$temp = 1;
foreach($range as $letters){
if(ucfirst($fruit[0]) == $letters){
$output[$letters][] = ucfirst($fruit);
$temp = 0;
break;
}
}
if($temp){
$output["#"][] = $fruit;
}
}
ksort($output);
To avoid these complications, you may use only one foreach loop and the built-in PHP function in_array:
foreach($aTest as $fruit){
$first = ucfirst($fruit[0]);
if(in_array($first, $range)){
$output[$first][] = ucfirst($fruit);
}
else{
$output["#"][] = $fruit;
}
}
ksort($output);
I would categorize them first using ctype_alpha() and then sorting the result by the array key:
$output = array();
foreach ($aTest as $word) {
$output[ctype_alpha($word[0]) ? strtoupper($word[0]) : '#'][] = $word;
}
ksort($output);

echoing foreach loop

i have the following code
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
foreach ($contents as $key => $itemline)
{
}
foreach ($fnamearray as $key2 => $fname)
{
echo ($fname);
echo ($itemline);
}
what i want to do is to have the first line of each file echo so the output looks like
fname[0},itemline[0],fname[1],itemline[1]
what i am getting with the following is just this
fname[0],fname[1],fname[2].... ect
h
Assuming the indexes will always match:
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('/home/b1396hos/public_html/ofwgkta.co.uk/dd_folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
for($i = 0; $i < count($contents); $i++)
{
echo $fnamearray[$i];
echo $contents[$i];
}
Since both arrays are simple, consecutive numeric indexed arrays, you can just use a for loop:
$l = max(count($fnamedata),count($contents));
for($i=0; $i<$l; $i++) {
$itemline = $contents[$i];
$fname = $fnamearray[$i];
// do stuff
}

Categories