Solved

PHP and different CSS class on comas inside string of numbers

Posted on 2012-03-27
2
348 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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 …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now