Jump to content

Regular expressions


Richard Yates

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.