PHP searching/formatting string dynmically - php

I have multiple different db strings, one for example is:
"Apples,Apples,Apples,Oranges,Apples,Oranges"
I want to represent each string dynamically as:
"I have Apples and Oranges. There are 6 in total."
Another string might be:
"Apples,Apples,Apples,Apples";
Should say:
"I have Apples. There are 4 in total."
How would I script this in PHP? Ty for help!

<?php
$str = 'Apples,Apples,Apples,Oranges,Apples,Oranges';
$arr = explode(',', $str);
echo 'I have '.implode(' and ', array_unique($arr))
. '. There are '.count($arr).' in total.';
This should be self explanatory but for reference see: explode, implode & array_unique

I made this test:
<?php
$data = "Apples,Apples,Apples,Oranges,Apples,Oranges";
$array = explode(",",$data); //array of elements
$count = count($array); //How many elements
$arr_unique = array_unique($array); //array of unique elements
echo "I have ".implode(" and ", $arr_unique). ". There are ".$count." in total.";
//var_dump($arr_unique); //For test
?>
PS: Check the fiddle: http://phpfiddle.org/main/code/3u4-e5v

Related

Search in a php string for a count of numbers

I have variables like this:
Example 1: 709000-037602-14-5_ABC
Example 2: 702000-025801-12_4_DEF
Example 3: 210104-1041-011866_GHI
How can i cut the data?
My idea is to seach for the underscore BUT the example 1 does cut on the false segment.
I want to get this variables: $var=709000-037602-14-4, $var=709000-037602-14_4 and $var1=210104-1041-011866
(ABC, DEF, GHI are not important. I only wanted to show i cant search for it.)
For now my used function is:
$var= strstr($var_original, '_', true);
Question: Can I count the number of numbers on the secound sequment (037602, 025801 or 1041) because this is 6 OR 4 digits.
Thanks for help :)
You can use:
explode
array_pop
implode
Like:
$string = '709000-037602-14-5_ABC';
$string1 = '702000-025801-12_4_DEF';
echo explodeCode($string).'<br>'. explodeCode($string1);
function explodeCode($code){
$final = explode('_', $code);
array_pop($final);
return (count($final) === 2) ? implode('_', $final) : implode('', $final);
}
Output:
709000-037602-14-5
702000-025801-12_4
After your multiple example i suppose is better use preg_split like:
$string = '709000-037602-14-5_ABC';
$string1 = '702000-025801-12_4_DEF';
$string2 = '709000-037602-14-5_ABC_test_no1';
echo explodeCode($string).'<br>'. explodeCode($string1). '<br>'. explodeCode($string2);
function explodeCode($code){
return preg_split("/\_[A-Z]/", $code)[0];
}
Output:
709000-037602-14-5
702000-025801-12_4
709000-037602-14-5

Is there a way to compare two strings where one is the parent string and the other a child string and make bold similar substrings?

I have two strings that look like this:
$string1 = "aaaa, bbbb, cccc, dddd, eeee, ffff, gggg";
$string2 = "aaaa, ffff";
I have extracted these strings by employing the function array_intersect in PHP and then imploding the resultant arrays into these two strings.
I would like to have elements in $string2 that appear in $string1 echoed out in bold without removing any element in $string1. For example i would like to have the following result echoed out in HTML:
aaaa, bbbb, cccc, dddd, eeee, ffff, gggg
I have implemented the following solution:
$array1 = explode(',', $string2):
foreach($array1 as $t){
$string1= str_replace($t,'<b>'. $t.'</b>',$string1);
}
echo "$string1";
My solution works but i would like to know if there is a better/efficient/cleaner way of achieving this using PHP?
explode-ing the strings back into arrays, so the longer string can be iterated and checking the short string, with in_array, for any matching items:
$string1 = "aaaa, bbbb, cccc, dddd, eeee, ffff, gggg";
$string2 = "aaaa, ffff";
$array1 = explode(", ", $string1);
$array2 = explode(", ", $string2);
$array3 = [];
foreach ( $array1 as $val ) {
if ( in_array($val, $array2) ) {
array_push($array3, "<strong>$val</strong>");
}
else {
array_push($array3, $val);
}
}
$string3 = implode(", ", $array3);
Try it here: https://onlinephp.io/c/431ec

Split strings with full name into just first name

I know this question has been answered in here before, but the different functions suggested in the other questions have not been to any help for me - and I´ve tried some few.
I have for example this string with three names that outputs from my database every time it's being loaded:
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
I only want it to output:
$string = "Joe, Carl, Miranda";
I tried this one: click - but it only outputs the first name in some situations and not every time. Is there a easy solution to this one? I also tried explode and implode but did not get that to work either.
Something like this?
<?php
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$names = explode(",", $string);
$firstNames = array_map(function($name) {
$split = explode(" ", trim($name));
return reset($split);
}, $names);
echo implode(", ", $firstNames);
First you can explode() string with comma , separated. Then go through the loop.
and get first name in the array with substr() and then implode with ,.
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$names = explode(",", $string);
$firstNames = array();
foreach($names as $name){
$firstNames[] = substr(trim($name), 0, strpos(trim($name), " "));
}
echo implode(", ", $firstNames);
First explode the string on ,, loop through the array, then push the elements in a new array and implode it.
$string = "Joe Hansen, Carl Clarkson Clinton, Miranda Cobweb Fisher-Caine";
$new_string=explode(",",$string);
$slipt_arr=explode(" ",$new_string);
$final_arr=array();
foreach ($new_string as $value)
{
$elements=array_filter(explode(" ",$value));
array_push($final_arr,current($elements));
}
echo implode(", ",$final_arr);

PHP find if numbers contained in a string are present in another string

Hi,
I have one string like this:
4,21
and the other like this:
,4,5,6,14,21,22,
I need to find out if any of the numbers contained in the first string are present in the second string but since each number is separated by a comma Im a little confused. What function should I use?
Thank you.
You could convert each string into arrays.
Try this:
$str1 = "4,21";
$array1 = explode(",", $str1);
$str2 = "4,5,6,14,21,22";
$array2 = explode(",", $str2);
$common = array_intersect($array1, $array2);
echo "Common numbers:<br/>";
echo implode(",", $common);

How to sort words in alphabetical order from a sentence, not array

I'm trying to take sentences from a user and storing them to compare them later, but i want them to be alphabetial, like;
Apple
Banana
taken from a string like, "Banana, Oranges And Apples"; So before that i first cut the words i don't want but i don't know how I'll go about sorting since PHP sort() seems to only work on array
<?php
$str = 'Man Methord Wifi HOlla Teddy Husband';
$result = trim( preg_replace(
"/[^a-z0-9']+([a-z0-9']{1,5}[^a-z0-9']+)*/i",
" ",
" $str "
) );
$lower_str = strtolower ($result);
echo $lower_str;
?>
This function may work for you :
function sortstring($string,$unique = false) {
$string = str_replace('.', '', $string);
$array = explode(' ',strtolower($string));
if ($unique) $array = array_unique($array);
sort($array);
return implode(' ',$array);
}
You can find this function and its example usage at http://www.wordinn.com/solution/226/php-sort-words-sentences
It has got an easy function that is called sort().In sort function, there are many type of different sort algorithms working on sort function.This function has got 2 parameters.First parameter of sort function is array which will be sort and Second one is about type of sort algorithm.
<?php
$data = array("a","k","b","z","i","v");
sort($data,SORT_STRING);
print_r($data);
?>

Categories