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. Quote 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 ) Quote 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. Quote 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 Quote 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. Quote 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? Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/56645-preg_replace-pattern-attribute/#findComment-279761 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.