user1188320
user1188320

Reputation: 575

how to use php add a class to the code?

<div class="centerBox">
<div class="centerBox line-y">
<div class="centerBox line_y">
<div class="centerBox">
<div class="centerBox line_y">
<div class="centerBox line_y">
<div class="centerBox">
<div class="centerBox line_y">
<div class="centerBox line_y">

I want to use loop to add a class named line_y to the above code, how should i do? thank you.

add line-y to 2,3,5,6,8,9 lines

Upvotes: 0

Views: 90

Answers (2)

tlenss
tlenss

Reputation: 2609

Not quite sure what you really want here. And how you are reading this chunk of HTML. Besides that the chunk of HTML code is not valid as displayed above. That being said.

You could just use a simple str_replace for this.

$str = '<div class="centerBox">
<div class="centerBox line-y">
<div class="centerBox line_y">
<div class="centerBox">
<div class="centerBox line_y">
<div class="centerBox line_y">
<div class="centerBox">
<div class="centerBox line_y">
<div class="centerBox line_y">';

echo str_replace('"centerBox"', '"centerBox line_y"', $str);

Upvotes: 0

Corubba
Corubba

Reputation: 2243

Use a for loop with a modulo division.

for($i=0; $i<10; $i++) {
  if( ($i % 3) === 0 )
    echo '<div class="centerBox">';
  else
    echo '<div class="centerBox line_y">';
}

The if condition matches line 0, 3, 6 and so on (first line is line 0).

Upvotes: 1

Related Questions