0

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jevgeni
  • 3
  • 1

2 Answers2

4

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.

janmoesen
  • 7,910
  • 1
  • 23
  • 19
  • BTW, I wholeheartedly agree with not using regular expressions for parsing HTML. For instance, this code does not work for single-quoted href='' attributes. Use DOMDocument::loadHTML, for instance. – janmoesen Mar 11 '10 at 12:08
  • I just love it when new users come here to get a quick answer and then vamoose! Also, I love the word "vamoose". – janmoesen Mar 19 '10 at 18:04
0

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.

Community
  • 1
  • 1
Kamarey
  • 10,832
  • 7
  • 57
  • 70
  • I do agree that using regexp on HTML is generally a bad idea, but when you only need to fetch a very specific string from a HTML document, like a single attribute, a regexp is fine. – Atli Mar 11 '10 at 12:11
  • Agree, it depends on a specific case. – Kamarey Mar 11 '10 at 12:13