Moi!
Mulla on vähän ongelmia tuon str_replacen kanssa.
Yritän saada kirjaimet <span> muotoon, siten että <span> tulostaa kirjaimesta vain kuvan.
str_replace kumminkin korvaa myös kaikki korvaavien tekstin.
Tässä kuva ongelmasta (vain pieni osa, en voi koko litanniaa kuvata)
http://gyazo.com/05b0f8fa60fa8483a24594606f85e9bd
Tässä tuo koodi
<?php $teskti = "Mites sulla menee?"; $korvattavat = array("A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", "Ä", "ä", "Ö", "ö", "Å", "å", ".", ",", "!", "?", "'"); $korvataan = array('<span id="letter1"></span>', '<span id="letter1"></span>', '<span id="letter2"></span>', '<span id="letter2"></span>', '<span id="letter3"></span>', '<span id="letter3"></span>', '<span id="letter4"></span>', '<span id="letter4"></span>', '<span id="letter5"></span>', '<span id="letter5"></span>', '<span id="letter6"></span>', '<span id="letter6"></span>', '<span id="letter7"></span>', '<span id="letter7"></span>', '<span id="letter8"></span>', '<span id="letter8"></span>', '<span id="letter9"></span>', '<span id="letter9"></span>', '<span id="letter10"></span>', '<span id="letter10"></span>', '<span id="letter11"></span>', '<span id="letter11"></span>', '<span id="letter12"></span>', '<span id="letter12"></span>', '<span id="letter13"></span>', '<span id="letter13"></span>', '<span id="letter14"></span>', '<span id="letter14"></span>', '<span id="letter15"></span>', '<span id="letter15"></span>', '<span id="letter16"></span>', '<span id="letter16"></span>', '<span id="letter17"></span>', '<span id="letter17"></span>', '<span id="letter18"></span>', '<span id="letter18"></span>', '<span id="letter19"></span>', '<span id="letter19"></span>', '<span id="letter20"></span>', '<span id="letter20"></span>', '<span id="letter21"></span>', '<span id="letter21"></span>', '<span id="letter22"></span>', '<span id="letter22"></span>', '<span id="letter23"></span>', '<span id="letter23"></span>', '<span id="letter24"></span>', '<span id="letter24"></span>', '<span id="letter25"></span>', '<span id="letter25"></span>', '<span id="letter26"></span>', '<span id="letter26"></span>', '<span id="letter27"></span>', '<span id="letter27"></span>', '<span id="letter28"></span>', '<span id="letter28"></span>', '<span id="letter29"></span>', '<span id="letter29"></span>', '<span id="letter30"></span>', '<span id="letter31"></span>', '<span id="letter32"></span>', '<span id="letter33"></span>', '<span id="letter34"></span>'); $ulos = str_replace($korvattavat, $korvataan, $teksti); echo $ulos; ?>
Mikäköhän voisi olla, ja saako korjattua, vai pitääkö käyttää toista functiota.
Kiitos vastauksista jo etukäteen!
Tuossa koko hommassa ei ole päätä eikä häntää. Jospa kertoisit, mitä oikein yrität tehdä, jotta joku voi sitten kertoa, miten se kannattaisi tehdä.
Siis tarkoitus olisi saada esim teksi näin, niin muotoon
<span id='letter14'></span><span id='letter27'></span><span id='letter9'></span><span id='letter14'></span>
Mutta tuo PHP korvaa myös noita $korvataan kohtia, jolloin teksti
tulee näin,
<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an> id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an>><span id="letter27"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an> id="<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an> id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>12"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an>>e<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>9"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>a<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an>><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>14"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span><<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>e<<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan id="le<span id="letter20"></span><span id="letter20"></span>er18"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>16"></<span id="le<span id="letter20"></span><span id="letter20"></span>er19"></span>pan>an>
Neuvoja?
Käytä korvaukseen funktiota preg_replace_callback.
$s = "abc"; $t = preg_replace_callback( "/[A-Za-z.,!?']|å|ä|ö|Å|Ä|Ö/", function($m) { switch ($m[0]) { case "a": case "A": return "<span ...>"; case "b": case "B": return "<span ...>"; default: return $m[0]; } }, $s );
Sama id saa olla vain yhdellä elementillä, joten HTML-koodisi on väärin.
Kiitos Metabolix, sehän toimii!
Kyllähän toi sun koodisi tarkoitus selvisi jo ekasta viestistä, mutta tarkoitinkin sitä, ettei sinun päämääräsi siitä selviä. Nopeasti mieleen tulee kaksi vaihtoehtoa: tekstin kryptaaminen ja oman fontin toteuttaminen. Kumpaakaan ei missään nimessä pidä lähteä tekemään noin.
Tästä on muuten jopa PHP:n sivuillakin maininta:
lainaus:
Caution
Replacement order gotchaBecause str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.
Itse en kyllä lähtisi millään pregeillä ja callbackeilla tekemään vaan looppaisin tekstin läpi ja muuntaisin jos osuu. Voisin melkein väittää, että menisi tehokkaammmin ja koodikin olisi selkeämpää.
feenix kirjoitti:
Itse en kyllä lähtisi millään pregeillä ja callbackeilla tekemään vaan looppaisin tekstin läpi ja muuntaisin jos osuu. Voisin melkein väittää, että menisi tehokkaammmin ja koodikin olisi selkeämpää.
UTF-8 kylläkin mutkistaa asiaa sen verran, että koodista ei luultavasti tule selvempää ja nopeuden suhteen saa nähdä vaivaa. Nopeus riippuu myös datasta: jos kaikki merkit muutetaan, silmukka voittaa selvästi (mutta preg_match_all pääsee jo lähelle), mutta jos huomattava osa merkeistä ei vaadi muunnosta, preg_replace_callback tulee tehokkaammaksi (esim. tekstin "aä123" toistolla jopa nopeimmaksi testikoodestani).
Tein vielä tällaiset nopeustestit:
<?php $testit = [ "preg_replace_callback" => function($a) { return preg_replace_callback( "/[A-Za-z.,!?']|å|ä|ö|Å|Ä|Ö/", function($m) { switch ($m[0]) { case "a": return "<span ...>"; case "ä": return "<span ...>"; default: return $m[0]; } }, $a ); }, "preg_match_all" => function($a) { preg_match_all("/[^\\x80-\\xbf][\\x80-\\xbf]*/", $a, $m); $b = ""; foreach ($m[0] as $tmp) { switch ($tmp) { case "a": $b .= "<span ...>"; break; case "ä": $b .= "<span ...>"; break; default: $b .= $tmp; } } return $b; }, "UTF-8 valikoivasti käsiteltynä" => function($a) { $b = ""; for ($i = 0; $i < strlen($a); ++$i) { if (ord($a[$i]) >= 0xc0) { ++$i; switch ($a[$i-1].$a[$i]) { case "ä": $b .= "<span ...>"; break; default: $b .= $a[$i-1].$a[$i]; } } else { switch ($a[$i]) { case "a": $b .= "<span ...>"; break; default: $b .= $a[$i]; } } } return $b; }, "UTF-8 täysin käsiteltynä" => function($a) { $b = ""; for ($i = 0; $i < strlen($a); ++$i) { $tmp = $a[$i]; if (ord($tmp[0]) >= 0xc0) $tmp .= $a[++$i]; if (ord($tmp[0]) >= 0xe0) $tmp .= $a[++$i]; if (ord($tmp[0]) >= 0xf0) $tmp .= $a[++$i]; switch ($tmp) { case "a": $b .= "<span ...>"; break; case "ä": $b .= "<span ...>"; break; default: $b .= $tmp; } } return $b; } ]; header("Content-Type: text/plain; charset=UTF-8"); foreach (["a", "ä", "aä", "aä1", "aä12", "aä123"] as $s) { $n = ceil(500000 / strlen($s)); $a = str_repeat($s, $n); $tulokset = []; foreach ($testit as $nimi => $funktio) { $t0 = microtime(true); $b = $funktio($a); $aika = microtime(true) - $t0; $tulokset[] = sprintf("%.3f s: %s\n", $aika, $nimi); } natsort($tulokset); $l = strlen($a); echo "$s * $n (len = $l):\n", implode($tulokset), "\n"; } readfile(__FILE__);
Tulokset:
a * 500000 (len = 500000): 0.310 s: UTF-8 valikoivasti käsiteltynä 0.514 s: preg_match_all 0.517 s: UTF-8 täysin käsiteltynä 0.551 s: preg_replace_callback ä * 250000 (len = 500000): 0.217 s: UTF-8 valikoivasti käsiteltynä 0.228 s: preg_match_all 0.298 s: preg_replace_callback 0.322 s: UTF-8 täysin käsiteltynä aä * 166667 (len = 500001): 0.248 s: UTF-8 valikoivasti käsiteltynä 0.317 s: preg_match_all 0.380 s: preg_replace_callback 0.393 s: UTF-8 täysin käsiteltynä aä1 * 125000 (len = 500000): 0.291 s: UTF-8 valikoivasti käsiteltynä 0.319 s: preg_replace_callback 0.359 s: preg_match_all 0.447 s: UTF-8 täysin käsiteltynä aä12 * 100000 (len = 500000): 0.286 s: preg_replace_callback 0.302 s: UTF-8 valikoivasti käsiteltynä 0.388 s: preg_match_all 0.473 s: UTF-8 täysin käsiteltynä aä123 * 83334 (len = 500004): 0.262 s: preg_replace_callback 0.317 s: UTF-8 valikoivasti käsiteltynä 0.404 s: preg_match_all 0.493 s: UTF-8 täysin käsiteltynä
Testeistä nähdään, että UTF-8-tekstin käsittely silmukassa on yleensä hitain vaihtoehto ja erikseen viritelty UTF-8-käsittely usein nopein (mutta koodissa epäselvin ja työläin). Syötteestä riippuen preg_match_all sijoittuu näiden väliin. Jos jokainen merkki on muutettava, preg_replace_callback on hitain, mutta jos yli puolet tekstistä menee ilman muunnoksia, preg_replace_callback nouseekin jo selvästi kärkeen.
Aihe on jo aika vanha, joten et voi enää vastata siihen.