Limit 2 digits only from a hash code
I have this $img['hash']
which is a long hash code 32-64 digits a09806aa003033128f44d5fd4c56786b (as an example)
Is there any way in php to pull only the 1st two digits (a0)
<?php
$cdb = new PDO('mysql:dbname=xxx;host=localhost', 'xxx', 开发者_如何学Python'xxx');
foreach ($cdb->query("SELECT * FROM images ORDER BY posted DESC LIMIT 9") AS $img)
{
echo '<a style="position: relative; display: block; height: 200px;" href="/booru/post/view/' . $img['id'] . '" target="_blank">
<img src="booru/timthumb.php?src=thumbs/' . $img['hash'] . '&h=125&w=125" style="border-style: none"/>
</a>
';
}
Sorry I should of added some more info I need to keep the $img['hash'] also
So like this
<img src="booru/timthumb.php?src=thumbs/' . $img['hash'] . '/Here/&h=125&w=125" width="125px" style="border-style: none"/>
I need to add it where the "/here/" is
You can use substr()
to get a substring of a string, in this case your hash:
$twofirst = substr($img['hash'], 0, 2);
Try:
substr($img['hash'], 0, 2);
Yes, use substr:
echo '<img src="...' . substr($img['hash'], 0, 2) . '" />';
You can use php's substr method (http://php.net/manual/en/function.substr.php) and output:
substr($img['hash'), 0, 2)
Which will output the first 2 digits, starting at index 0, of the $img['hash'] string.
In PHP you can use substr http://php.net/manual/en/function.substr.php
substr(img['hash'], 0, 2)
should do the trick
精彩评论