PHP preg_replace .*?

Started by
2 comments, last by Colin Jeanne 18 years, 9 months ago
Hey everyone. I would like to know how to use preg_replace to make it return anything in between
<!-- DESC --> ..some words and <!-- /DESC -->
. Like for example I have a document and it contains
<!-- DESC -->Welcome, this is a test <!-- /DESC -->
I want my function to return Welcome, this is a test. How would I do this? Thanks in advance.
Advertisement
If you're looking for matches preg_replace isnt the right function for the job. preg_match() or preg_match_all() is better.
no I'm wanting to replace that data with <textarea>${0}</textarea>
Alright. I'll show you with single strings since that's the simplest. It's not hard to extend it to using arrays for the search string and the replace string.

// I assume you're getting your string from a file. It doesnt really matter$f = fopen('your_file', 'r');$text = fread($f, filesize('your_file'));// What this does is searches for the entire block for your DESC tag// The inside of the block is put in parentheses so that I can backreference// it in the replacement. The $1 is the reference to everything in the parentheses$new_text = preg_replace('/<!-- DESC -->(.*)<!-- \/DESC -->/', '$1', $text);

This topic is closed to new replies.

Advertisement