如何在已经深入语法时添加字符串深度

问题描述:

Php code:

 <?php echo '<h1 class="woning'.$counter.' woningen"onClick="javascript:changeText(this);$("#projects woningproject"'.$counter.'").slideToggle(300)">>' .$row->Projecttitel. '</h1>'; ?>

This will generate

<h1 class="woning1 woningen" woningproject"1"").slidetoggle(300)"="" #projects="" onclick="javascript:changeText(this);$(">>Wijveld</h1>

Why ? Because the first " before #projects closes the onChange tag.. I have no idea how to fix my " & 's to do this, as i seemingly need to use something that is a level deeper than that.

Try this:

<?php echo '<h1 class="woning'.$counter.' woningen" onClick="javascript: changeText(this); $(\'#projects woningproject'.$counter.'\').slideToggle(300)">' .$row->Projecttitel. '</h1>'; ?>

You just need to escape the nested quotes for it to work.

<?php

$counter = 123;

$row = new stdClass();
$row->Projecttitel = 'SampleProjecttite';

// Here onwards
$class = sprintf('woning%s woningen', $counter);
$click = sprintf('javascript:changeText(this); $(\'#projects woningproject%s\').slideToggle(300)', $counter);

echo sprintf('<h1 class="%s" onclick="%s">%s</h1>', $class, $click, $row->Projecttitel);

Is this what you are trying to do?

Here we go:

<?php echo '<h1 class="woning'.$counter.' woningen" onClick="javascript:changeText(this);$(\'#projects .woningproject'.$counter.'\').slideToggle(300)">' .$row->Projecttitel. '</h1>'; ?>

Notice dot in front of "woningproject" class because w/o dot you targeting tag instead class.