I'm trying to replace some custom strings in my content before saving the post. I have got the $data["post_content"] which includes strings like %replaceContent:{type}% . Now I need to extract that string, read the {type} and replace the string afterwards depending on what's inside of {type}. I imagine this would best be done using regex, unfortunately I don't really know how to go about this. Ideas?
1 Answer
This belongs to stackoverflow. Here's a solution anyway:
$content = preg_replace_callback('/\%replaceContent:{(.*?)}\%/', 'do_replacements', $content); function do_replacements($matches){ $type = $matches[1]; // here's your {type} $replacement = "...replacement for {$type}"; return $replacement; }