1

I read the following text out of a Mssql database:

  1. Leitwert und Korrosionsschutz des Kühlwassers überprüfen: 111- Umrichterkühlkreis < 200µS/cm 111- Ofenkühlkreis und IGBT-Anlagen: < 400µS/cm 111- Konzentration des Korrosionsschutzes

I want to replace "111" with a new line ("<br>") using this replace function:

str_replace("111", "<br>", $text);

or this one

array_replace("111", "<br>", $text);

Both lines work when I declare the text just in the code, but if I read it from the database it doesn't work. I use utf8_encode after reading this text from db.

I think the problem is the datatype which is defined as "text" in the table of the database.

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
Felix A.
  • 25
  • 1
  • 5
  • Why are you using the `text` datatype in the first place? It's been deprecated since at least SQL Server 2005. You should really be using `varchar(MAX)`. – Thom A Dec 18 '18 at 08:59
  • Can't you just load it all into an array then loop and format it to your liking? – Isaac Dec 18 '18 at 09:03
  • This may be a [good Q&A to read through](https://stackoverflow.com/questions/3786003/str-replace-on-multibyte-strings-dangerous) – RiggsFolly Dec 18 '18 at 09:04

2 Answers2

1

You can use the preg_replace() function.

$string = 'Leitwert und Korrosionsschutz des Kühlwassers überprüfen: 111- Umrichterkühlkreis < 200µS/cm 111- Ofenkühlkreis und IGBT-Anlagen: < 400µS/cm 111- Konzentration des Korrosionsschutzes';

$string = preg_replace('/111/', '<br>', $string);

echo $string;

This will output:

Leitwert und Korrosionsschutz des Kühlwassers überprüfen:
- Umrichterkühlkreis < 200µS/cm
- Ofenkühlkreis und IGBT-Anlagen: < 400µS/cm
- Konzentration des Korrosionsschutzes
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
  • Glad it helped! ~Cheers – Joseph_J Dec 18 '18 at 09:13
  • Sorry but I'm unable to see why this code should be have differently than the one in the question. Isn't it the the same byte-level search and replace? – Álvaro González Dec 18 '18 at 09:32
  • 1
    @ Álvaro González I do not have an answer for you as I believe you are correct as to the same byte-level operations. Either the OP changed his code to mirror the flow of my code, or did not utf8_enode the data prior to feeding it through the functions, or there is a difference between the two functions with his current set up. I was thinking about this too. I would also like to point out that the OP's assumption was an encoding error, when in fact it may not be. – Joseph_J Dec 18 '18 at 10:08
0

utf8_encode() is a poorly named function that's often used having no clue of its actual purpose. If you work with SQL Server, it's very unlikely that you have ISO-8859-1 strings coming from the database because:

  1. Microsoft's RDBM works natively with two encodings at the same time, a Unicode-compatible one (if NTEXT column type) and ANSI (if TEXT column type). "ANSI" is just a generic term for "Windows default codepage in the computer where the server runs" and it can be something like Windows-1252 but not ISO-8859-1.

  2. PHP database extensions typically convert database strings from/to the application encoding and they do it transparently (they also do it correctly if you set connection settings properly).

So it's very likely that's the source of your problems.

Last but not least, str_replace() is a legacy function that isn't multi-byte aware. Unfortunately, it doesn't have a multi-byte equivalent but you can find alternatives out there.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360