I have a TXT file formatting that looks like:
123451234512345
123451234512345
I want to format the file with php in this format:.
12345-12345-12345
12345-12345-12345
This is what I have tried:
$codes = "codes.txt";
$unformatedCode = file($codes, FILE_IGNORE_NEW_LINES);
$abcd = "-";
foreach ($array as $value) {
$text = (string)$unformatedCode;
$arr = str_split($text, "");
$formatedCode = implode("-", $arr);
echo $formatedCode;
}
Try this,
<?php
$arr = array_map(
fn($v) => implode('-', str_split($v, 5)),
file("codes.txt", FILE_IGNORE_NEW_LINES)
);
print_r($arr);
Result:
Array
(
[0] => 12345-12345-12345
[1] => 12345-12345-12345
)
If you want to echo the result then do <?= implode(PHP_EOL, $arr) ?>
Possible that if can be written shorter
$codes = "codes.txt";
$array = file($codes, FILE_IGNORE_NEW_LINES);
$p = "/([1-9]{5})([1-9]{5})([1-9]{5})/i";
$r = "${1}-${2}-${3}";
foreach ($array as $a) {
echo preg_replace($p, $r, $a);
}
Related
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;
?>
multiple Array Explode,i Want Output Using Explode This Type
1,1000,AA
2,2000,BB
3,3000,CC
<?php
$data1= "1,2,3";
$data2= "1000,2000,3000";
$data3= "AA,BB,CC";
$array = explode(',', $data1);
foreach ($array as $data1)
{
echo $data1;
echo $data2;
echo $data3."<br>";
}
?>
multiple Array Explode
I Want Output Using Explode
1 1000 AA
2 2000 BB
3 3000 CC
All you need to do is explode each of your original data items into an array and then process one array using the index to also reference the other 2 arrays.
<?php
$data1= "1,2,3";
$data2= "1000,2000,3000";
$data3= "AA,BB,CC";
$arr1 = explode(',', $data1);
$arr2 = explode(',', $data2);
$arr3 = explode(',', $data3);
foreach ( $arr1 as $key => $val ) {
echo sprintf( '%s %s %s<br>', $val, $arr2[$key], $arr3[$key] );
}
RESULTS:
1 1000 AA<br>2 2000 BB<br>3 3000 CC<br>
Or if seen in a browser
1 1000 AA
2 2000 BB
3 3000 CC
Use this code it will help you
If there is space between string (After AA,BB) then use space (" ") in explode and if there is new line then use \n in explode
<?php
$str = "1,1000,AA 2,2000,BB 3,3000,CC";
$arr = explode(" ",$str);
print_r($arr);
$arr1 = array();
$arr2 = array();
$arr3 = array();
foreach($arr as $val){
$value = explode(",",$val);
$arr1[] = $value[0];
$arr2[] = $value[1];
$arr3[] = $value[2];
}
print_r($arr1);
print_r($arr2);
print_r($arr3);
?>
$data1= "1,2,3";
$data2= "10,20,30";
$data3= "100,200,300";
$arr1 = explode(',', $data1);
$arr2 = explode(',', $data2);
$arr3 = explode(',', $data3);
foreach ( $arr1 as $key => $val ) {
echo $val;
echo $arr2[$key];
echo $arr3[$key];
}
How do I convert the array
Array
(
[1] => a,b,c
[2] => x,y,z
)
into an associative array like
Array
(
[a]=> b,c
[x]=> y,z
)
Basically want to convert value of an array into a key.
How about this:
$arr = array('a,b,c','x,y,z');
$newArr = array();
foreach($arr as $key => $value) {
$value = explode(",",$value);
$firstValue = $value[0];
array_shift($value);
$newArr[$firstValue] = implode(",",$value);
}
print_r($newArr); //Array ( [a] => b,c [x] => y,z )
A faster solution:
foreach($array as $item){
$x = explode(',',$item);
$new_array[$x[0]] = implode(','array($x[1],$x[2]));
}
print_r($new_array);
Try out this,
$newArray = array();
foreach($array as $data){
$values = explode(",",$data);
$key = array_shift($values);
$newArray[$key] = implode($values,",");
}
print_r($newArray);
DEMO.
Do this:
$myArray=array(1=>'a,b,c', 2=>x,y,z);
foreach($myArray as $val){
$Xval=explode(",",$val);
$newKey=$Xval[0];
unset($Xval[0]);
$newArray[$newKey]=implode(",",$Xval);
}
Try like
$res = array();
foreach($my_arr as $value)
{
$my_var[] = explode(',',$value);
$i = 0;
foreach($my_var as $ky)
{
if($i++ != 0)
$exp_arr[] = $ky;
}
$res[$my_var[0]] = implode(',',$exp_arr);
}
or you can unset like
foreach($my_arr as $value)
{
$my_var[] = explode(',',$value);
$temp = $my_var[0];
unset($my_var[0]);
$res[$temp] = implode(',',$my_var);
}
try this
<?php
$array=array('a,b,c', 'x,y,z');
foreach($array as $key=>$val)
{
$array[substr($val,0,1)]=substr($val,2);
unset($array[$key]);
}
print_r($array);
?>
See Demo
My ips.txt file contains the following content:
radie1230: 116.79.254.131
Y_O_L_O: 122.149.157.42
midgetman63: 121.121.14.101, 124.112.115.69, 114.182.51.1, 114.118.55.131, 111.21.22.156
kypero: 121.211.61.118, 117.117.117.46, 121.214.109.247, 111.219.37.75
lythorous: 111.161.225.214, 12.111.184.71, 1.112.201.113, 11.137.214.184, 1.115.21.117, 12.115.241.212, 11.117.116.217
This list contains usernames on the left, separated by : from IPs on the right. IPs are separated by ,.
What would be an implementation to create the following output?
Array (
[midgetman63] => Array
(
[0] => 121.121.14.101
[1] => 124.112.115.69
[2] => 114.182.51.1
[3] => 114.118.55.131
[4] => 111.21.22.156
)
)
Try this:
$dataFromFile = file('ips.txt');
$dataFromFile = array_map('trim', $dataFromFile);
$result = array();
foreach ($dataFromFile as $line) {
list($user, $ips) = explode(':', $line, 2);
$arrayOfIPs = explode(',', $ips);
$result[trim($user)] = array_map('trim', $arrayOfIPs);
}
var_dump($result);
if you dont have spaces at the end of the textfile, this should be work.
good luck.
$text = file_get_contents("ipsontext.txt");
$array = explode("\n",$text);
$finally_array = array();
foreach ($array as $key => $value) {
$this_key = explode(":",$value);
$this_values = explode(",",$this_key[1]);
foreach($this_values as $tv) {
$finally_array[$this_key[0]][] = $tv;
}
}
var_dump($finally_array);
Maybe this?
<?php
$output = array();
$search = 'something';
$lines = file('file.txt');
foreach($lines as $line) {
if(strpos($line, $search) !== false) {
$d = explode(':', $line);
$s = explode(',', $d[1]);
foreach $s as $i { array_push($output, $i); }
}
}
print_r($output);
?>
In the following code, is it possible to spilt the Object $author where white space occurs ?
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
echo $a;
}
?>
$split = explode(' ', (string) $key->{"author"}));
OR
$split = preg_split('/\s+/', (string) $key->{"author"}));
To split by # just take $split and run in loop
foreach($split as $key => $value) {
$eta = explode('#', $value);
var_dump($eta);
}
To check if string exist use strpos
foreach($split as $key => $value) {
if (strpos($value, '#') !== 0) echo 'found';
}
Use explode:
$array = explode(' ', $key->{"author"});
There is an explode function that can easily accomplish that. So, for example:
$a = $key->{"author"};
$author = explode(" ", $a);
$first_name = $author[0];
$last_name = $author[1];
Hope that helps.
Assuming you merely care to get 2 parts: email, and "friendly name" (cause people have 1 to n number of names).
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
preg_match("/([^ ]*) (.*)/", $a, $matches);
print_r($matches);
echo "\n";
}
?>