How can I replace characters with preg_replace, that are enclosed in quotes. I need to replace all special characters, that are in href="" things. example:
<a href="ööbik">ööbik</a> should become <a href="oobik">ööbik</a>
How can I replace characters with preg_replace, that are enclosed in quotes. I need to replace all special characters, that are in href="" things. example:
<a href="ööbik">ööbik</a> should become <a href="oobik">ööbik</a>
To replace the "special chars", you need to use iconv: $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
As for getting the values in between the quotes, see the other answers. Use preg_replace_callback
to execute the conversion above on the matches.
EDIT: spoon-feeding everything together:
<?php
$input = 'ööbik';
$expected = 'ööbik';
// Set the locale of your input here.
setlocale(LC_ALL, 'en_US');
// Convert using a callback.
$output = preg_replace_callback('/href="([^"]+)"/', function ($matches) {
return iconv('UTF-8', 'ASCII//TRANSLIT', $matches[0]);
}, $input);
echo "Input: $input\n";
echo "Expected: $expected\n";
echo "Output: $output\n";
This example assumes PHP 5.3. Use "create_function" or a named function if you are stuck on PHP 5.2 or below.
While this question may help you finding quoted text: http://stackoverflow.com/questions/2148587/regex-quoted-string-with-escaped-quotes-in-c/2150049
I think the better solution is to do this by parsing an html string and work with its DOM.
I'd asked first, wha do you need such conversion?
And wouldn't urlencode()
fit better?