Trying to replace this tag in a post : Lorem ipsum dolor sit amet, consectetur adipisicing elit, [tag]13231[/tag] sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. PHP: I want to get 13231 and replace it with other text.
Try this. untested $content = preg_replace('/\[tag\](.*)\[\/tag\]/','$something_else',$content); PHP: EDIT: opps I had an extra apostrophe, try it now.
You should consider making the quantifier non-greedy, if there was ever multiple [tag]'s in the content - it would only match once. Like this: <?php $string = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, [tag]13231[/tag] sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; $replacement_text = 'other text'; $string = preg_replace('#\[tag\](.*?)\[/tag\]#', $replacement_text, $string); echo $string; ?> PHP: