I have a file containing data something like this:
randomString-data1
randomString-data2
randomString-data4
randomString-data2
randomString-data2
randomString-data3
randomString-data4
The output I want:
randomString-data1
randomString-data2
randomString-data4
randomString-data3
The length of random string is between 20 to 25 characters. The randomString is not important. The data after it is of relevance only. What could be the best way to remove such duplicates?
All I can think of is the brute force way to explode each line at "-" and compare with all others. But is there a better way?
Note: The random strings are important. But it is okay if any one of the randomString remains from the duplicate entries.
Explode and array flip and then get array keys
$content_array = explode($file_contents);
//Remove duplicate entries
$flipped_array = array_flip($content_array);
file_put_contents('/path/to/file', implode("\n", array_keys($flipped_array));
firstly explode them by '\n' then by '-'
$arr = explode('\n', $str);
$data = array();
foreach($arr as $val)
{
$temp = explode("-", $val);
$data[$temp[1]] = $temp[0];
}
$str = "";
foreach($data as $k => $v)
{
$str .= $v . "-" . $k;
}
i would do it this way
<?php
$file = <<<EOF
randomString-data1
randomString-data2
randomString-data4
randomString-data2
randomString-data2
randomString-data3
randomString-data4
EOF;
$found = [];
foreach (explode("\n", $file) as $line) {
list(, $date) = explode('-', $line);
if (!isset($found[$date])) {
echo "$line<br>";
$found[$date] = true;
}
}
?>
Related
$string = file_get_contents('./story.txt', true);
$words = explode('\n', $string);
$w = array();
foreach ($words as $word) {
$temp = explode(' ', $word);
$w = array_merge($w, $temp);
}
$longestWordLength = 0;
$longestWord = '';
foreach ($w as $word) {
if (strlen($word) > $longestWordLength) {
$longestWordLength = strlen($word);
$longestWord = $word;
}
}
echo $longestWord;
echo strlen($longestWord);
I have written this code but it scans the ending word from one para and first word from next paragraph as same. In the following para:
arts along the stream looks almost like a flash of sunlight.
Desert animals are generally the color of the desert.
In this,
sunlight. Desert
is treated as one word.
You can try this way to get the longest string from the file text, I've removed extra spaces and dot characters from the array using array_map()
function longest_string_in_array($array) {
$array = array_map(function($item) { return trim($item, '. ');},$array);
$mapping = array_combine($array, array_map('strlen', $array));
return $mapping;
}
$string = file_get_contents('./story.txt', true);
$array = preg_split('/[\s]+/', $string );
print_r(longest_string_in_array($array));
So, I guess the answer to this handsome boy's problem is that those words are not splitting apart that are separated by anything other than single whitespace. In this case the solution is to use,
$string = file_get_contents('./story.txt', true);
$words = preg_split('/\s+/', $string);
and you're good to go.
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']);
}
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";
}
?>
My data variable:
1=icon_arrow.gif,2=icon_arrow.gif,3=icon_arrow.gif
I need it to be grouped by the value after =, so in this case it should look like this:
$out = array('icon_arrow.gif' => '1, 2, 3');
The 'icon_arrow.gif' field need to be unique, and can't repeat.
How it can be done?
Initialize an array:
$array = array();
Explode the input by ,, store results as an array based on a sub-explode of it by =:
foreach(explode(',', $string) as $p)
{
list($i, $n) = explode('=',$p);
$array[$n][] = $i;
}
Implode then the result with ,:
foreach($array as &$v)
$v = implode(', ', $v);
unset($v);
Done.
Just for fun. With no special chars this should also work.
$str="1=icon_arrow.gif,2=icon_arrow.gif,3=icon_arrow.gif,4=x.gif";
$str = str_replace(',', '&', $str);
parse_str($str, $array);
$result = array();
foreach($array as $k=>$v)
$result[$v] = (isset($result[$v]) ? $result[$v] . ", " : "") . $k;
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;