while saving in database, if I save A it shouldnot save as A, it should save as 1, for b as 2, it should continue till 26.
How it is possible?

Recommended Answers

All 20 Replies

What about something like:

<?php
$letters = range('a', 'z');
$numbers = range(0, 26);

echo 'Value before: ' . $value . '<br>';
$value = strtolower($value); // The value in which you want to replace stuff.
$value = str_replace($letters, $numbers);
echo 'Value after: ' . $value . '<br>';

How to use default as defined in PHP myadmin

Thank you minitauros

Member Avatar for diafol

Shouldn't the range be range(1,26) ?

Here's my take...

function convert($value)
{
    $keys = range("A","Z");
    $values = range(1, 26);
    $falseValue = 0; //or false or null etc- decide
    $conv = array_combine($keys,$values);
    $value = strtoupper($value); //remove this if not req'd
    return (isset($conv[$value])) ? $conv[$value] : $falseValue;
}

$val = 'C';
$myVal = convert($val);
echo "'$val' gives = $myVal <br />";

Non-array functions tend to be quicker though.

Then how to do?

You're right, my bad. You cannot set it as a default in PHPMyAdmin/MySQL. You will either have to create a stored procedure for it or edit the data you save every time you save it using PHP (I'd suggest the latter option).

Member Avatar for diafol

I was confused too as this is posted in the PHP forum. If this has to be a MySQL flavoured SQL solution, it should be posted to the Databases > MySQL forum

Non-array functions tend to be quicker though.

What do you mean, exactly?

Ok Thank you for both of you

Member Avatar for diafol

What do you mean, exactly?

Your string functions should be faster than my array_combine(). However thinking about it, you're using arrays in your string functions too, so not sure. Be interesting to do a time comparison.

Ah, yes, I'd be curious to know that as well.

Member Avatar for diafol

Here's the test...

$start = microtime(true);
function convert($value)
{
    $keys = range("A","Z");
    $values = range(1, 26);
    $falseValue = 0; //or false or null etc- decide
    $conv = array_combine($keys,$values);
    $value = strtoupper($value); //remove this if not req'd
    return (isset($conv[$value])) ? $conv[$value] : $falseValue;
}

echo convert('C');
$end = microtime(true);

echo "<br /><br />$end - $start = " . ($end - $start) . "<br /><br />";

$start = microtime(true);
$letters = range('a', 'z');
$numbers = range(1, 26);
$value = strtolower('C'); // The value in which you want to replace stuff.
$value = str_replace($letters, $numbers,$value);
echo $value;

$end = microtime(true);

echo "<br /><br />$end - $start = " . ($end - $start);

Nothing in it. BTW - you need to place $value in the str_replace()

I've slightly edited the test, and it resulted - as you suspected - in that my solution is twice as fast on average.

function convert($value)
{
    $keys = range("A","Z");
    $values = range(1, 26);
    $falseValue = 0; //or false or null etc- decide
    $conv = array_combine($keys,$values);
    $value = strtoupper($value); //remove this if not req'd
    return (isset($conv[$value])) ? $conv[$value] : $falseValue;
}

$start = microtime(true);
for ($i = 0; $i <= 100000; $i++) {
    convert('C');
}
$end_1 = microtime(true);
echo "<br /><br />$end_1 - $start = " . number_format(($end_1 - $start), 3, '.', '');
echo '<div style="clear: both; height: 0px; height: 2px; background-color: #000; border-top: 1px dashed #fff; border-bottom: 1px dashed #fff;"></div>';


$start = microtime(true);
$letters = range('a', 'z');
$numbers = range(1, 26);
$value = strtolower('C'); // The value in which you want to replace stuff.
for ($i = 0; $i <= 100000; $i++) {
    str_replace($letters, $numbers, $value);
}
$end_2 = microtime(true);
echo "<br /><br />$end_2 - $start = " . number_format(($end_2 - $start), 3, '.', '');

$percentage = number_format(min($end_1, $end_2) / max($end_1, $end_2) * 100, 2, '.', '');
echo "<p>Solution " . ($end_1 > $end_2 ? '2' : '1') . " is $percentage% faster.</p>";
Member Avatar for diafol

Excellent. I forgot the loop - doh! Thought it was quick - heh heh!

Yes, bravo - one-up for the string manipulation.

Thank you diafol and minitauros

Member Avatar for diafol

I love string functions - I can't stop tinkering with them - they're great fun. Here's an example of another one using minitauros' str_replace...

function replace($value, $toCase=null, $in=null, $out=null)
{
    $in = ($in) ? $in : range('A','Z');
    $out = ($out) ? $out : range(1,26);
    if($toCase && in_array($toCase, array('upper','lower')))
        $value = call_user_func( 'strto' . $toCase, $value );
    return str_replace($in, $out, $value);
}

echo replace('A'); //returns 1

echo replace('b'); //returns b

echo replace('b', 'upper'); //returns 2

echo replace('A', 'lower'); //returns a

echo replace('C', null, null, range(0,25)); //returns 2 as expected

What would it matter if you either upper- or lowercase the whole string before converting it? The only thing that matters here is the output, right? :) Which would be the same regardless of if you either upper- or lowercase the input value. (I can't think of an example in which I would find it useful, do you have one?)

Member Avatar for diafol

Changing case before is also an option of course. Apples and pears. I offered that as a plaything - just to keep the client code as uncluttered as possible.

Oh yeah I wasn't trying to say anything bad about your code, I was just really wondering if it would matter uppercasing or lowercasing the whole string before converting it :).

commented: no worries +15
Member Avatar for diafol

Oh yeah I wasn't trying to say anything bad about your code

I realise that, don't worry. I've got a hide like a hippo anyway! I can't see it matters too much about the "placing of the casing".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.