Solved

PHP and different CSS class on comas inside string of numbers

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

862 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

22 Experts available now in Live!

Get 1:1 Help Now