xnowandtheworldx Posted April 11, 2008 Share Posted April 11, 2008 I'm making a script, and I need to replace every symbol, like, `, ~, , , !, @, ETC. ETC. but instead of just replacing everyone if there are two in a row i still need it to only replace the symbols with only 1 "_"...and also remove any "_" that would be at the end after being replaced...heres an example... The.losers! => the_losers The..losers!! => the_losers php!!rules:D => php_rules_D so any help would be nice..i didn't really know how to explain lol. Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/ Share on other sites More sharing options...
darkfreaks Posted April 11, 2008 Share Posted April 11, 2008 use preg_replace. <?php $pattern="`~!@"; $replace=""; preg_replace($pattern,$replace,$string);?> Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515141 Share on other sites More sharing options...
xnowandtheworldx Posted April 11, 2008 Author Share Posted April 11, 2008 Thank you very much! Never really understood preg_replace , but thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515142 Share on other sites More sharing options...
darkfreaks Posted April 11, 2008 Share Posted April 11, 2008 if this is solved please hit topic solved Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515143 Share on other sites More sharing options...
xnowandtheworldx Posted April 11, 2008 Author Share Posted April 11, 2008 Eh, Warning: preg_replace() [function.preg-replace]: No ending delimiter '`' found in /home/a5206629/public_html/bruce/test_r.php on line 5 $pattern="`~!@"; $replace = "the..losers..!~"; preg_replace($pattern,$replace,$string); echo $string; the line with the error is the preg_replace(); line Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515148 Share on other sites More sharing options...
effigy Posted April 11, 2008 Share Posted April 11, 2008 What do you want to allow rather than exclude? Every PREG pattern requires delimiters. Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515154 Share on other sites More sharing options...
xnowandtheworldx Posted April 11, 2008 Author Share Posted April 11, 2008 basically, i need to allow all numbers and letters, but all symbols need to be replaced with an underscore Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515156 Share on other sites More sharing options...
effigy Posted April 11, 2008 Share Posted April 11, 2008 Only ASCII letters? preg_replace('/[^a-z\d]+/i', '_', $string); Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515157 Share on other sites More sharing options...
discomatt Posted April 11, 2008 Share Posted April 11, 2008 Here's my shot at it. <?php $regex = '/[\W_]++/'; // Match one or more characters that is a non-word character or underscore as many times as possible $subject = 'Hello, my name\'s Matt! I _hate_ non-word..characters'; $result = preg_replace($regex, '_', $subject); echo $result; // Outputs: // Hello_my_name_s_Matt_I_hate_non_word_characters ?> If you want to maintain underscores (ie __ not converted to _) just change the regex to this $regex = '/[\W]++/'; Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-515160 Share on other sites More sharing options...
aCa Posted April 14, 2008 Share Posted April 14, 2008 A small coment. I agree that the pattern /[\W]+/ is the best to use. But we need one more regex. You stated you didn't wan't _ at the end of words. You will need to do a replace with a pattern like this: /_(\s)|_$/ That will remove _ when space or end of line after it. Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-516656 Share on other sites More sharing options...
effigy Posted April 14, 2008 Share Posted April 14, 2008 /_(\s)|_$/ This is better written as /_(?:\s|$)/ since they share the underscore and there's no need to capture. Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-516714 Share on other sites More sharing options...
xnowandtheworldx Posted May 11, 2008 Author Share Posted May 11, 2008 Alright thanks for all the replies guys, and sorry about not replying till now, got caught up doing something and ended up forgetting about this thread >.< lol. Thanks again guys really helped! Quote Link to comment https://forums.phpfreaks.com/topic/100722-solved-replacing-symbols-help/#findComment-538158 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.