turtleman8605 Posted June 22, 2007 Share Posted June 22, 2007 Can someone try to explain the pattern attribute in the preg_replace() function? I'm trying to get it to replace a line of html with an updated line of new html, and I just don't understand how to use that parameter. Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/ Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 ereg_replace ( string $pattern, string $replacement, string $string ) Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279743 Share on other sites More sharing options...
turtleman8605 Posted June 22, 2007 Author Share Posted June 22, 2007 I know how it's set up. I don't understand what the pattern parameter is used for or what I should put there. Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279748 Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 give us the sample string or read this http://www.php.net/manual/en/function.ereg-replace.php Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279752 Share on other sites More sharing options...
btherl Posted June 22, 2007 Share Posted June 22, 2007 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. Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279755 Share on other sites More sharing options...
turtleman8605 Posted June 22, 2007 Author Share Posted June 22, 2007 So if I want to replace an entire string, then pattern is the same as the replacement string? Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279757 Share on other sites More sharing options...
trq Posted June 22, 2007 Share Posted June 22, 2007 So if I want to replace an entire string, then pattern is the same as the replacement string? No. That pattern must match wont you want to replace. Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.