Solved

PHP and different CSS class on comas inside string of numbers

Posted on 2012-03-27
2
350 Views
Last Modified: 2012-08-13
Hi all.

I am in need of expert help with PHP and CSS. I am trying to create a count ticker with a different css class assigned to the coma in a number string. The number string get pulled from my database.

For example, my number is 11,229,228 how could I have PHP apply different css class to each individual digit and each individual coma.

The finished code should look like this:

<div style="float:left">
<div class="number">1</div>
<div class="number">1</div>
<div class="coma">,</div>
<div class="number">2</div>
<div class="number">2</div>
<div class="number">9</div>
<div class="coma">,</div>
<div class="number">2</div>
<div class="number">2</div>
<div class="number">8</div>
</div>

Open in new window


I have tried to preg_replace(); option, but do not seem to get my head around to make it work.
0
Comment
Question by:slickip
2 Comments
 
LVL 10

Accepted Solution

by:
MadShiva earned 500 total points
ID: 37771651
Dear,

This work :

<?php

$str = '11,229,228';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

echo '<div style="float:left">';

foreach ($chars as $char) {

	if(is_numeric($char))
	{	
	echo "<div class=\"number\">$char</div>";
	}

	if($char==",")
	{	
	echo "<div class=\"coma\">,</div>";
	}	

}

echo '</div>';

?>

Open in new window



EDIT : forgot the first div with float

Regards
0
 

Author Closing Comment

by:slickip
ID: 37771716
PERFECT! That was a very fast reply indeed!

Thank you, your help was appreciated.

Regards.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question