digitalecartoons Posted November 28, 2006 Share Posted November 28, 2006 Does anyone know what the author is trying to prevend by using double backslashes? Here's part of the text:[code]$joketext = eregi_replace('\\[b]','<strong>',$joketext);$joketext = eregi_replace('\\[eb]','</strong>',$joketext);Notice that, because [ normally indicates the start of a set of acceptable characters in a regular expression, we put a backslash before it in order to remove its special meaning. As backslashes are also used to escape special characters in PHP strings, we must use a double backslash for each single backslash that we with to have in the regular expression.[/code]So why is this wrong:[code]$joketext = eregi_replace('\[b]','<strong>',$joketext);[/code]Is there any reason why I should use double backslashes? Perhaps he's trying to prevend some unicode code inside the regex string? In that case, which code would stand for \ [ b ]? Link to comment https://forums.phpfreaks.com/topic/28727-double-backslashes/ Share on other sites More sharing options...
printf Posted November 28, 2006 Share Posted November 28, 2006 No it does nothing. A lot of times you might see double backslashes because the person may have copied the example off a forum or other script that had problems with regex patterns used for display purposes. vBulletin, phpBB, IPB, fudforum, SMF and other blog/wiki type systems had this problem one time or another, where it would escape the escape twice, which would show the extra back slash. By default PHP only removes slashes for characters that might match a set of special circumstance's, for example...[code]echo "\"hi\"'; // prints "hi"[/code]or this example...[code]<?php$str = '\[b\]';echo $str; // prints \[b\]$str = '\\[b\]'; // special circumstance, escape an escape \, not needed, just for the exampleecho $str; // prints \[b\]?>[/code]Also PHP handling of escape character sequences has changed, over time, in early versions of PHP doing this...[code]echo "\[b\]";[/code]Would have resulted in it printing [b], as PHP did the same thing Perl still does when you use double quotes, but PHP has changed that, which to me is [b]wrong[/b], because double quotes, means anything in the string value should interpolate, which includes any single \, should be removed. While using single quotes, no single \, should ever be removed, because single quotes should never interpolate.printf Link to comment https://forums.phpfreaks.com/topic/28727-double-backslashes/#findComment-131566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.