Reputation: 831
I'd like to use a random id
attribute in HTML:
<?php $id = uniqid(); ?>
<div data-cycle-prev="#<?php echo $id; ?>_slideshow_prev">
<div id="<?php echo $id; ?>_slideshow_prev"></div>
</div>
I'm using PHP method uniqid
to generate it, but I don't know if it is safe and W3C compliant?
My concern for compliance is for the generated HTML which the PHP outputs. Not all characters are allowed in an HTML attribute, so I was wondering if uniqid()
generates only allowed characters.
Upvotes: 4
Views: 1858
Reputation: 17024
Yes it is save, as long you do not generate the id twice times, which is very unlikly.
I would do it another way. Maybe you want to introduce this in to your library
function getNextInt() {
static $i = 0;
$i++;
return $i;
}
And in Your Template:
<?php $id = getNextInt(); ?>
<div data-cycle-prev="#<?php echo $id; ?>_slideshow_prev">
<div id="<?php echo $id; ?>_slideshow_prev"></div>
</div>
If you are using older Versions of HTML, we need to have a letter first as id (statet from @mb21 in the comments):
<?php $id = 'mycontrol' . getNextInt(); ?>
Upvotes: 2
Reputation: 5332
uniqid
returns id, which contains hexadecimal symbols(0-9, a-f). This is explained in manual comments - http://php.net/manual/en/function.uniqid.php#95001
So, yes, this is safe for html attributes.
Upvotes: 3