jevgeni
jevgeni

Reputation: 3

Replace characters that are enclosed with quotes using a regular expression

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>

Upvotes: 0

Views: 488

Answers (2)

Kamarey
Kamarey

Reputation: 11079

While the Stack Overflow question Finding quoted strings with escaped quotes in C# using a regular expression may help you finding quoted text, I think the better solution is to do this by parsing an HTML string and work with its DOM.

Upvotes: 0

janmoesen
janmoesen

Reputation: 8020

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.

Upvotes: 4

Related Questions