Richard Yates Posted August 20, 2011 Share Posted August 20, 2011 I am trying to learn about regular expressions and preg_replace() starting with the absolutely simplest cases but something is not working (most likely my brain). Here is code and output: <?php $str="abcef"; $str2 = preg_replace("[a-d]","x", $str); echo '$str='.$str.' '.'$str2='.$str2; ?> $str=abcdef $str2=abcdef Why is $str2 not: xxxxef ? Quote Link to comment https://forums.phpfreaks.com/topic/245253-regular-expressions/ Share on other sites More sharing options...
Alex Posted August 20, 2011 Share Posted August 20, 2011 PCREs require delimiters. For example, /[a-d]/, or ~[a-d]~. Quote Link to comment https://forums.phpfreaks.com/topic/245253-regular-expressions/#findComment-1259642 Share on other sites More sharing options...
Richard Yates Posted August 20, 2011 Author Share Posted August 20, 2011 Thanks. Fixed. Quote Link to comment https://forums.phpfreaks.com/topic/245253-regular-expressions/#findComment-1259646 Share on other sites More sharing options...
.josh Posted August 20, 2011 Share Posted August 20, 2011 btw you can technically use [..] as the delimiter like so: <?php $str="abcef"; $str2 = preg_replace("[[a-d]]","x", $str); echo '$str='.$str.' '.'$str2='.$str2; ?> ...but I wouldn't recommend it, since brackets have special meaning in regex (used for character class), so it makes for less readable code. Quote Link to comment https://forums.phpfreaks.com/topic/245253-regular-expressions/#findComment-1259708 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.