php - Translate non-digital characters to specific digits

174

Is it possible to convert characters in a string into specific numbers like

a = 1,  // uppercase too
b = 2,  
c = 3,  
d = 4,  
e = 5,  // and so on til letter 'z'

space = 0 // I'm not sure about if space really is equals to 0

Here's how I think it goes.

$string_1 = "abed";   // only string
$string_2 = "abed 5"; // with int

$result_1 = convert_to_int($string_1); // output is 1254
$result_2 = convert_to_int($string_2); // output is 125405
583

Answer

Solution:

Create an array, and insert a space to the first element. Then userange() to generate an array witha toz. Usestrtolower() to force the input to lowercase (as the characters fromrange() we generate is lowercase too.

Then do a replacement withstr_replace(), which accepts arrays as values. The keys is the value that the value will be replaced with.

function convert_to_int($string) {;
    $characters = array_merge([' '], range('a', 'z'));
    return str_replace(array_values($characters), array_keys($characters), $string);
}
84

Answer

Solution:

Here is the complete code:

$s = 'abcde';
$p = str_split($s);
foreach($p as $c) {
    echo ord($c) - ord('a') + 1;
}
690

Answer

Solution:

To use the numbers that you have showna = 1 etc... then just do a case-insensitive replace:

$result = str_ireplace(range('a', 'z'), range(1, 26), $string);

If you want the ASCII value then split to an array, map to theord value and join:

$result = implode(array_map(function($v) { return ord($v); }, str_split($string)));
114

Answer

Solution:

To perform a string translation, the most appropriate native function isstrtr().

If you don't need to translate to double digit numbers, to can pass two correlated strings.

Code: (Demo)

echo strtr('abcde 5', ' abcde', '012345');
// 1234505

If you need to accommodate the full alphabet, then declare an associative array of string keys to numeric values. The map can be manually written or populated with a variety of native functions.

Code: (Demo)

$map = [];
array_push($map, ' ', ...range("a", "z"));
echo strtr('abcde 5', array_flip($map));
// 1234505

Or as a one-liner:

echo strtr('abcde 5', array_flip(array_merge([' '], range("a", "z"))));

To accommodate uppercase charactets, simply callstrtolower() on the input string before translating.

Relevant reading:

995

Answer

Solution:

Using regex should be like this:

$search  = array('/[A-a]/', '/[B-b]/', '/[C-c]/', '/[D-d]/', '/[" "]/');
$replace = array('1', '2', '3', '4', '5');

$final = preg_replace($search, $replace,"abcd ABCD a55");

echo $final;

Output: 1234512345155

People are also looking for solutions to the problem: php - Display data from text file

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.