|
// Define a dummy text, for testing...
$ Text = "Title: Hello world! \ N ";
$ Text. = "Author: Jonas \ n ";
$ Text. = "This is a example message! \ N ";
$ Text. = "Title: Entry 2 \ n ";
$ Text. = "Author: Sonja \ n ";
$ Text. = "Hello world, what's up! \ N ";
// This function will replace specific matches
// Into a new form
Function RewriteText ($ Match ){
// Entire matched section:
// --> /.../
$ EntireSection = $ Match [0];
// --> "\ NTitle: Hello world! "
// Key
// --> ([A-z0-9] +)
$ Key = $ Match [1];
// --> "Title"
// Value
// --> ([^ \ N \ r] +)
$ Value = $ Match [2];
// --> "Hello world! "
// Add some bold () Tags to around the key
Return''. $ Key .': '. $ Value;
}
// The regular expression will extract and pass all "key: value" pairs
// The "RewriteText" function that is definied above
$ NewText = preg_replace_callback ('/[\ r \ n] ([a-z0-9] +): ([^ \ n \ r] +)/I', "RewriteText ", $ Text );
// Print the new modified text
Print $ NewText;
|