Highlight the text string in HTML CSS with php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for a quick way to highlight a text string if it has a flag i.e.(-R) as a first symbol in it, from the beginning till the line end (\n).
The function will be given a text of a multiple strings separated with "\n" that may or may not contain any flags at all. Something like this:
-Rnotes to be red
-Ynotes to be yellow
-G notes to be green
In the output of this function I need to get this:
<span style="background-color:red">notes to be red</span>
<span style="background-color:yellow">notes to be yellow</span>
<span style="background-color:green"> notes to be green</span>

Based on Matt's answer I made this answer.
Keep in mind it's Matt's idea it's based from so don't forget to upvote his answer if you like this.
I use str_replace to replace -R (or Y or G) with the span tag and use the -R as the key in color array.
Then I just add closing span tag.
$notes = array(
'-Rnote',
'-Gnote',
'-Ynote',
'-Rnote1',
'-Gnote1',
'-Ynote1',
);
// Above array can be replaced by:
// $notes = explode("\n", $text);
$colors = array(
'-R' => 'red',
'-G' => 'green',
'-Y' => 'yellow',
);
foreach ($notes as $note ) {
If (isset($colors[substr($note,0,2)])){
echo str_replace(substr($note, 0, 2), '<span style="background-color:' . $colors[substr($note, 0, 2)] . '">', $note) . "</span>\n";
}Else{
Echo $note ."\n";
}
}
https://3v4l.org/9YDAX
Edit noticed how I forgot that there may not be a color tag in the string.
Added a echo of no color notes

You could try something link this:
$notes = array(
'-Rnote',
'-Gnote',
'-Ynote',
'-Rnote1',
'-Gnote1',
'-Ynote1',
);
$colors = array(
'R' => 'red',
'G' => 'green',
'Y' => 'yellow',
);
foreach ($notes as $note ) {
$color_key = substr($note, 1, 1);
$note_string = substr($note, 2);
$bg_color = array_key_exists($color_key, $colors) ? $colors[$color_key] : '';
echo '<p style="background-color:' . $bg_color . '">' . $note_string . '</p>';
}
Storing your colors in an associative array may make it easy to add more colors in the future.
This might be a little messy, but it should work for you.

Related

extracting numbers from in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to create an array of integers from strings of text composed of the integers separated by whitespace and plus signs, for example
$string = "1 + 2 + 3 + 4";
is extracted into
$array = ('1' , '2' ,'3' , '4');
This needs to be done in php.
Thank you in advance for any help.
Thankyou for the question refinment.
<?php
$string = '1 + 2 + 3';
$array = explode(' + ', $string);
for ($a=0;$a<count($array);$a++){
$array[$a] = (int) $array[$a]; // RECAST STRING TO INTEGER
}
?>
To get the array you need to use PHPs explode function, which will split a string into an array by a delimiter, for example:
$integerString = '1 + 2 + 3 + 4';
$integerArray = explode(' + ', $integerString);
Use preg_match_all!
<?php
$string = '1 + 2 + 3';
preg_match_all("'[0-9]+'sim", $string, $out);
print_r($out);

How to count complete sets of items in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am a newbie, so hopefully I word this properly. I have searched and searched both SO and the web to try to find this answer, even mathematically, to understand how to code it, but to no avail.
On a web game, I want to be able to count the number of complete sets a user has (the set has four items).
For example, the user can attain turkey, pumpkin pie, stuffing, and mashed potatoes. Those are displayed in a table by quantity, like so:
Mashed Potatoes: 2
Turkey: 4
Pumpkin Pie: 4
Stuffing: 3
So in this case, the user has 2 total sets of what I'll call a "Thanksgiving Dinner." The Thanksgiving Dinner must include one of each item to be a complete set.
How can I use PHP to count and display the number of complete sets the user has?
This feels like it should be so simple (mathematically, at least), but I am really struggling.
Try this, simple, as shown in this PHP Fiddle
<?php
$min = 100000; // just initialize min with large value.
// these values can be hard coded, from JSON, html form, or database
// it is just written for illustration
$arr = array(
'Mashed Potatoes' => 2,
'Turkey' => 4,
'Pumpkin Pie'=> 4,
'Stuffing'=> 3
);
// we loop through each element of the above array and,
// compare its value to the min.
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
?>
EDIT:
As suggested in a comment from PaparazzoKi, Thanks, you can replace all of this:
foreach($arr as $value){
if( $value < $min){
// If the value is less that min. then we store value into min
$min = $value;
}
}
echo 'You can have ' . $min . ' Thanksgiving dinners';
With this line:
echo 'You can have ' . min($arr) . ' Thanksgiving dinners';
Making use of the .min() function
The total number of complete set will the the key with the lowest value in the array.
You may use the min() function of PHP
http://php.net/manual/en/function.min.php

Setting a custom base in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to build a custom base that consist of 31 characters:
$custom_array = array("0","1","2","3","4","5","6","7","8","9",
"b","c","d","f","g","h","j","k","l","m","n"
,"p","q","r","s","t","v","w","x","y","z");
I excluded vowels from the alphabet and added numbers at the begining.
Well, basically i need a function that is similar to base_convert(), so that i can convert to base10 and vice versa.
Thanks for any help.
Edit:
I came up with this function but the accepted answer is also a good approach. Thanks.
$basearray = array("0","1","2","3","4","5","6","7","8","9","b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z");
function baseXto10($code,$basearray)
{
$codearr = array_reverse(str_split($code));
$charnum = count($basearray);
$sum = 0;
$i = 0;
foreach($codearr as $key => $value)
{
$charpos = array_search($basearray, $value);
$sum+= $charpos * pow($charnum,$i);
$i++;
}
return $sum;
}
You probably want to use base_convert() for converting between actual bases. Then, from base10 (or any for that matter) map the values between the initial and your new base.
Perhaps something like this
function my_base_convert($input, $fromBase)
{
$baseMapping = [
'0' => 0,
'a' => 'b',
//etc
];
$result = base_convert($input, $fromBase, 10);
return str_replace(array_keys($baseMapping), array_values($baseMapping), $input);
}
Then simple str_replace

Need a preg_replace or best option for this conversion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm sure this is simple and I have tried formatting several combinations my example S1, Ep1 = 10001 or S1, Ep2 = 10002 preg_ combinations and all fails, not my forte'
The format i need is as follows:
$link = array('Episodes from link');
$str = preg_replace('/^s/+', '0', $link);
echo $str(array(
'10001',
'10002',
'10003',
));
and so on...
Keep getting to many zeros because of the empty, comma and the characters, any help is greatly appreciated...
Thanks
Your question is very unclear and I may be way off, but here is my attempt to your question.
$link = array('S1, Ep1', 'S1, Ep2', 'S1, Ep3', 'S1, Ep10');
$results = preg_replace_callback('~^S(\d+)\D+(\d+)$~m',
function($m) {
$which = strlen($m[2]) > 1 ? '00' : '000';
return $m[1] . $which . $m[2];
}, array_values($link));
print_r($results);
Output
Array
(
[0] => 10001
[1] => 10002
[2] => 10003
[3] => 10010
)

PHP convert ARGB to RGBA? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Need solution or description of: How to convert color value from ARGB to RGBA which is possible to use for css:
Examples:
ARGB color : #ff502797 convert to RGBA
RGBA result example: rgba(80,39,151,1)
thx.
ARGB contains 4 groups of HEX values(channels): Alpha, Red, Green, Blue.
Scheme:
Exampe: #ff502797 - color in ARGB formatt
Elements on positions:
0,1 - Alpa (ff)
2,3 - Red (50)
4,5 - Green (27)
6,7 - Blue (97)
After this convert each channel to decimal. And putt on their places into RBGA:
rgba(Red,Green,Blue,Alpha) - rgba(80,39,151,1)
Example function:
function argb2rgba($color)
{
$output = 'rgba(0,0,0,1)';
if (empty($color))
return $output;
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 8) { //ARGB
$opacity = round(hexdec($color[0].$color[1]) / 255, 2);
$hex = array($color[2].$color[3], $color[4].$color[5], $color[6].$color[7]);
$rgb = array_map('hexdec', $hex);
$output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')';
}
return $output;
}

Categories