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 1


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; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.