Forum

[SOLVED] Change the...
 
Notifications
Clear all

[SOLVED] Change the color of a div, based on specific letters with php ?

6 Posts
2 Users
0 Likes
528 Views
(@subzero)
Posts: 149
Member
Topic starter
 

Hi guys,

Need some help with php. This is my code:


<div class="letter-block">MS</div>
<div class="letter-block">JD</div>
<div class="letter-block">KS</div>
<div class="letter-block">JR</div>
<div class="letter-block">GW</div>
<div class="letter-block">FE</div>

What i need is to change the background color of the div's above, based on the first letter of the word they contain. For example if the first letter is M then the background color of the div must be #ff0088, if the first letter is K then the then the background color of the div must be #ff0000 etc.

Any idea ?

Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist

 
Posted : 28/10/2019 1:16 am
 fog
(@fog)
Posts: 116
Estimable Member
 

Hi, you need tell more details about process. Why need be like... detecting first letter to change the background color? Explain the reason.

Of course, directly you can add different class names with space separator.
examples: class="letter-block MS", class="letter-block KS". With some php conditional added, will display what you need.

But if you are using a php function is better add HTML inside function with variables to your background colors, with your conditionals.

For other hand, I no ideia if these DIVs will appears all together on same page, or only one will be displayed, and if will be something randomized, etc.

Explain better your idea / process if you can.

 
Posted : 28/10/2019 1:59 am
(@subzero)
Posts: 149
Member
Topic starter
 

Hi [member=67]fog[/member]. I was testing some ideas for the new theme of osclass and i just need to get the initial letters of some strings and print them in loop. However, finally i used the php function

preg_split

and then the

$string[0]

and worked perfect. The only problem i have is with non-latin characters. Tried many ways like mb_split but no luck. The output is like that

��

.

Anyway i 'll try to figure it out. (I hope lol).

Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist

 
Posted : 28/10/2019 9:12 pm
 fog
(@fog)
Posts: 116
Estimable Member
 

I never passed by that situation before with non-latin chars.
Maybe this comments can help in something, or not..
https://www.php.net/manual/en/function.utf8-encode.php#45226

Good luck with theme, I hope you can solve it 🙂

 
Posted : 28/10/2019 11:13 pm
(@subzero)
Posts: 149
Member
Topic starter
 

OK finally i made it. Everything works perfect!!!! PHP rocks  8) Thank u [member=67]fog[/member] 

Frontend Dev / UX-UI Designer / Illustrator / Father / Linux Enthusiast / Minimalist

 
Posted : 29/10/2019 10:14 pm
 fog
(@fog)
Posts: 116
Estimable Member
 

You're welcome!
The merit is totally yours 🙂

 
Posted : 30/10/2019 4:54 pm
Share: