Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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; 
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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