TheDev
TheDev

Reputation: 415

to manipulate a character with php

I need to manipulate a string that has a special caratectere (em dash), I have this string in a variable alta. — The ambient temperature is quite high and in no way can I use the explode from it, either with explode ('-', $ str) or using chr('-'), I just need to have it as a point to apply my logic but I can not

here is how it is displayed in html ""

the way it is displayed in the DOM:

enter image description here

Here I looped to display each letter in a line followed by the unicode with ord($str)

enter image description here

Here is the code I used to display a letter on each line and get that black character that I have no idea whatsoever

for($i = 0; $i<= strlen($str); $i++) {
    echo"<br>";
            echo ord($str[$i]);
            echo"<br>";
            echo $str[$i];
}

I want to get the index of this character in the string or simply use the explode method from this character

Upvotes: 0

Views: 236

Answers (1)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21671

This is what I do to that "stuff"

$search = array(
        "\xe2\x80\x98", // "'"
        "\xe2\x80\x99", // "'"
        "\xe2\x80\x9c", // '"'
        "\xe2\x80\x9d", // '"'
        "\xe2\x80\x93", // '-'
        "\xe2\x80\x94", // '-'
        "\xe2\x80\xa6", // '...'
        chr(145),
        chr(146),
        chr(147),
        chr(148),
        chr(150),
        chr(151),
        chr(133)    
    );

    $replace = array(
        "'",
        "'",
        '"',
        '"',
        '-',
        '-',
        '...',
        "'",
        "'",
        '"',
        '"',
        '-',
        '-',
        '...'
    );

    $text = str_replace($search, $replace, $text);

Nothing but a headache. Probably pasted from MSWord or such.

Test it:

 $text = 'alta. —  The ambient temperature is quite high';

 echo $text."\n";

 //... the above code ...

 echo $text."\n";

Output

alta. —  The ambient temperature is quite high
alta. -  The ambient temperature is quite high

Sandbox

Normally I just make a function somewhere, throw that stuff into it and clean it up. Then it's a normal - hyphen and everything works as expected.

I made this like 7 or 8 years ago, still use it. It's like MSWIN1252 charset or such.

Upvotes: 2

Related Questions