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 ? 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]~. 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. 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. Link to comment https://forums.phpfreaks.com/topic/245253-regular-expressions/#findComment-1259708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.