Jump to content

preg_replace "pattern" attribute


turtleman8605

Recommended Posts

Here's a simple example

 

$str = "Fluffy Bunnies";
$str = preg_replace("|Fluffy|", "Blood-thirsty", $str);
# Now $str is "Blood-thirsty Bunnies"

 

Slightly more complex

 

$str = "<b>Fluffy</b> Bunnies";
$str = preg_replace("|<.*>|", "Blood-thirsty", $str);
# Again, $str is "Blood-thirsty Bunnies", but for a totally different reason.

 

.* means "Any string of any characters".  So it matches the longest string starting with "<" and ending with ">".  These patterns are all identical:

 

"/<.*>/"

"+<.*>+"

 

It doesn't matter what you put at the start and end, but it must be the same each time.

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.