By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

Processing text / regexps

P: n/a
I'm trying to work out how to process a block of text which contains tags
similar to [link]http://something.com[/link]. I want to work through the
text block and pass the contents of each tag to function and then carry on
through the text when the function returns. I don't necessarily want to
change the text block, I think if I did it would make it easier as then I
could use a loop though the changing block with a regexp to find the next
unchanged tag until it could find no matches. Can anyone suggest a method of
doing it without changing the text block?

Many thanks,
Geoff
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40**********************@news-text.dial.pipex.com...
I'm trying to work out how to process a block of text which contains tags
similar to [link]http://something.com[/link]. I want to work through the
text block and pass the contents of each tag to function and then carry on
through the text when the function returns. I don't necessarily want to
change the text block, I think if I did it would make it easier as then I
could use a loop though the changing block with a regexp to find the next
unchanged tag until it could find no matches. Can anyone suggest a method of doing it without changing the text block?


Use preg_replace_callback(). No looping. No changing the original text.

function TurnIntoAnchorTag($matches) {
$url = htmlspecialchars($matches[1]);
return "<a href=\"$url\">"$url"</a>";
}

$new_text = preg_replace_callback('/[link](.*?)[\/link]/i',
'TurnIntoAnchorTag', $text);
Jul 17 '05 #2

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<pu********************@comcast.com>...
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40**********************@news-text.dial.pipex.com...
I'm trying to work out how to process a block of text which contains tags
similar to [link]http://something.com[/link]. I want to work through the
text block and pass the contents of each tag to function and then carry on
through the text when the function returns. I don't necessarily want to
change the text block, I think if I did it would make it easier as then I
could use a loop though the changing block with a regexp to find the next
unchanged tag until it could find no matches. Can anyone suggest a method

of
doing it without changing the text block?


Use preg_replace_callback(). No looping. No changing the original text.

function TurnIntoAnchorTag($matches) {
$url = htmlspecialchars($matches[1]);
return "<a href=\"$url\">"$url"</a>";
}

$new_text = preg_replace_callback('/[link](.*?)[\/link]/i',
'TurnIntoAnchorTag', $text);


Brilliant, thanks very much
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.