1

i have a string, like this:

'<indirizzo>Via Universit\E0 4</indirizzo>'

Whit HEX char... i need string become:

'<indirizzo>Via Università 4</indirizzo>'

So, i use:
$text= preg_replace('/(\\\\)([a-f0-9]{2})/imu', chr(hexdec("$2")), $text);

But dont work because hexdec dont use value of $2 (that is 'E0'), but use only value '2'. So hexdex("2") is "2" and chr("2") isnt "à"

What can i do?

0

3 Answers 3

1
$text='<indirizzo>Via Universit\E0 4</indirizzo>';

function cb($match) {
    return html_entity_decode('&#'.hexdec($match[1]).';');
}
$text= preg_replace_callback('/\\\\([a-f0-9]{2})/imu', 'cb', $text);

echo $text;
Sign up to request clarification or add additional context in comments.

Comments

1

You need to specify your chr(hexdec()) as a callback. Just calling those functions and suppling the result as parameter to preg_replace doesn't do it.

preg_replace_callback('/\\\([a-f0-9]{2})/imu',
                      function ($match) { return chr(hexdec($match[1])); },
                      $text)

Having said that, there are probably better ways to do what you want to do overall.

Comments

0

You could also use

<?php
$str = preg_replace('/\\([a-f0-9]{2})/imue', '"\x$1"', '<indirizzo>Via Universit\E0 4</indirizzo>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.