Smilies über PHP/Sql vor der ausgabe begrenzen******!

Hi, mit diesem Code klappt es:
PHP:
<?php 
$string = "wortA :(( :)):( :D :) :D wortB :D :(( wortC :)) :( :) :D:)) :(( wortD :D :(( :)) :) :( :D"; 
$regex = "/((( ?:D ?)|( ?:\(\( ?)|( ?:\( ?)|( ?:\)\) ?)|( ?:\) ?)){1,3})(( ?:D ?)|( ?:\(\( ?)|( ?:\( ?)|( ?:\)\) ?)|( ?:\) ?))*/"; 
$replace = "\1"; 
$newString = preg_replace ($regex, $replace, $string, -1 ); 
?>
Ergebnis:
Code:
wortA :(( :)):( wortB :D :(( wortC :)) :( :) wortD :D :(( :))
(getestet via http://regexp-tester.mediacix.de/exp/regex/)
Ich wage mich mal vor, ohne es getestet zu haben (geht grad leider nicht):
PHP:
<?php 
function SmilieSwap($string) {

$smiley_codes = array();
$abfrage = mysql_query('SELECT * FROM icc_smilie WHERE chat = "'.$chatadresse.'"');

while ($row = mysql_fetch_object($abfrage)) {
 
	// macht aus :)) dieses \:\)\), und aus :D dieses \:D
	// neben das D müsstest du noch all in deinen Smileys vorkommenden nicht-Regex-Zeichen packen, z.B. [^DEFG]
	$smiley_codes[] = preg_replace("([^D])", "\\\1", "( ?".$row->text." ?)"); // ergibt ( ?\:\) ?)

}  

$allsmiley_regex = implode("|", $smiley_codes); // ergibt ( ?\:\) ?)|( ?\:\( ?)|( ?\:\)\) ?) usw.
$threesmiley_regex = "/(".$allsmiley_regex."{1,3})".$allsmiley_regex."*/"; // ergibt den "$string" aus erstem PHP-Code
return preg_replace ($threesmiley_regex, "\1", $string, -1 ); 

}  
 
$message = "wortA :(( :)):( :D :) :D wortB :D :(( wortC :)) :( :) :D:)) :(( wortD :D :(( :)) :) :( :D"; 
$message = SmilieSwap($message); 
echo $message;
?>
 
Zuletzt bearbeitet:
Hallöchen...

Z.z noch

Code:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 5

greets
 
Tja, dann wirst du dein Problem wohl nicht lösen können!

OOOODER:

Du baust ein paar echos in meinen Code ein, um zu sehen, wie das Regex zusammengebaut wird.
Besonders für das $threesmiley_regex über das sich die Warning beschwert.
 
Vielleicht versteh ich ja was falsch aber wäre es nicht sinnvoller einfach preg_replace mit den limit parameter = 3 zunehmen.
Also ungefähr so:
PHP:
<?php
$smile = array (':)', ':))', ';)', ':(', ':((', ':D');

$abfrage = mysql_query('SELECT * FROM icc_smilie WHERE chat = "'.$chatadresse.'"');

while ($row = mysql_fetch_array($abfrage)) {
    $replacement[] = $row['text'];
}

foreach($smile as $val) {
    $pattern[] = "~".preg_quote($val,"~")."~i";
}
$message = preg_replace($pattern, $replacement, $message, 3);

//wenn du willst das die anderen smilies entfernt werden kannst du es so noch machen:
$message = str_replace($smile, '', $message);
 
Zuletzt bearbeitet:
Hallöchen... Da er irgendwie nichts ausgibt als ein weißes Fenster wollt ich mal fragen ob es normal ist, wenn ich:

Code:
print_r ($replacement);

ausgebe, er mir nur:

Code:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => [55] => [56] => [57] => [58] => [59] => [60] => [61] => [62] => [63] => [64] => [65] => [66] => [67] => [68] => [69] => [70] => [71] => [72] => [73] => [74] => [75] => [76] => [77] => [78] => [79] => [80] => [81] => [82] => [83] => [84] => [85] => [86] => [87] => [88] => [89] => [90] => [91] => [92] => [93] => [94] => [95] => [96] => [97] => [98] => [99] => [100] => [101] => [102] => [103] => [104] => [105] => [106] => [107] => [108] => [109] => [110] => [111] => [112] => [113] => [114] => [115] => [116] => [117] => [118] => [119] => [120] => [121] => [122] => [123] => [124] => [125] => [126] => [127] => [128] => [129] => [130] => [131] => [132] => [133] => [134] => [135] => [136] => [137] => [138] => [139] => [140] => [141] => [142] => [143] => [144] => [145] => [146] => [147] => [148] => [149] => [150] => [151] => [152] => [153] => [154] => [155] => [156] => [157] => [158] => [159] => [160] => [161] => [162] => [163] => [164] => [165] => [166] => [167] => [168] => [169] => [170] => [171] => [172] => [173] => [174] => [175] => [176] => [177] => [178] => [179] => [180] => [181] => [182] => [183] => [184] => [185] => [186] => [187] => [188] => [189] => [190] => [191] => [192] => [193] => [194] => [195] => [196] => [197] => [198] => [199] => [200] => [201] => [202] => [203] => [204] => [205] => [206] => [207] => )

als wenn er nichts bei

Code:
$abfrage['text'];

findet... gebe ich stadessen "test" ein gibt er mir:

Code:
$replacement[] = "test";

Code:
Array ( [0] => test [1] => test [2] => test [3] => test [4] => test [5] => test [6] => test [7] => test [8] => test [9] => test [10] => test [11] => test [12] => test [13] => test [14] => test [15] => test [16] => test [17] => test [18] => test [19] => test [20] => test [21] => test [22] => test [23] => test [24] => test [25] => test [26] => test [27] => test [28] => test [29] => test [30] => test [31] => test [32] => test [33] => test [34] => test [35] => test [36] => test [37] => test [38] => test [39] => test [40] => test [41] => test [42] => test [43] => test [44] => test [45] => test [46] => test [47] => test [48] => test [49] => test [50] => test [51] => test [52] => test [53] => test [54] => test [55] => test [56] => test [57] => test [58] => test [59] => test [60] => test [61] => test [62] => test [63] => test [64] => test [65] => test [66] => test [67] => test [68] => test [69] => test [70] => test [71] => test [72] => test [73] => test [74] => test [75] => test [76] => test [77] => test [78] => test [79] => test [80] => test [81] => test [82] => test [83] => test [84] => test [85] => test [86] => test [87] => test [88] => test [89] => test [90] => test [91] => test [92] => test [93] => test [94] => test [95] => test [96] => test [97] => test [98] => test [99] => test [100] => test [101] => test [102] => test [103] => test [104] => test [105] => test [106] => test [107] => test [108] => test [109] => test [110] => test [111] => test [112] => test [113] => test [114] => test [115] => test [116] => test [117] => test [118] => test [119] => test [120] => test [121] => test [122] => test [123] => test [124] => test [125] => test [126] => test [127] => test [128] => test [129] => test [130] => test [131] => test [132] => test [133] => test [134] => test [135] => test [136] => test [137] => test [138] => test [139] => test [140] => test [141] => test [142] => test [143] => test [144] => test [145] => test [146] => test [147] => test [148] => test [149] => test [150] => test [151] => test [152] => test [153] => test [154] => test [155] => test [156] => test [157] => test [158] => test [159] => test [160] => test [161] => test [162] => test [163] => test [164] => test [165] => test [166] => test [167] => test [168] => test [169] => test [170] => test [171] => test [172] => test [173] => test [174] => test [175] => test [176] => test [177] => test [178] => test [179] => test [180] => test [181] => test [182] => test [183] => test [184] => test [185] => test [186] => test [187] => test [188] => test [189] => test [190] => test [191] => test [192] => test [193] => test [194] => test [195] => test [196] => test [197] => test [198] => test [199] => test [200] => test [201] => test [202] => test [203] => test [204] => test [205] => test [206] => test [207] => test )

und bei

Code:
print_r ($message);

Code:
Array ( [0] => ttestt [1] => ttestt [2] => ttestt [3] => test [4] => test [5] => test )

aus.

greets
 
Zuletzt bearbeitet:
Zurück