Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

A function with ereg to find data between particular characters



1:58 pm on Jan 24, 2008 (gmt 0)

5+ Year Member

Trying to sort out a function that will recognise some custome tags, kind of like BBcode on forums I guess, and then replace the tag with some code.

Tags will be like -#THIS#-

I've setup a function codemagic() which returns some code in response to a value passed into it, so I can achieve the correct result as follows:

$pagecontent = str_replace('-#header#-',codemagic('-#header#-'),$pagecontent);
$pagecontent = str_replace('-#footer#-',codemagic('-#footer#-'),$pagecontent);
$pagecontent = str_replace('-#other#-',codemagic('-#other#-'),$pagecontent);

However, rather than having a str_replace going on for every section of code I'd like to achieve this with a single str_replace

I'm just hoping someone can point me in the right direction to do this

I'm imagining It'll involve some ereg function but I'm having trouble finding resources to find data between specified characters as opposed to matching an expression


4:24 pm on Jan 24, 2008 (gmt 0)

5+ Year Member

I've tried

$pagecontent = eregi_replace("\\-#([^\\[]*)\\#-", $phpsnip->codemagic("\\1"), $pagecontent);

but I get a blank output, any ideas?


4:06 am on Jan 25, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

With the PCRE [php.net] you could do something like this:
function codemagic($code) 
switch ($code) {
case '-#header#-':
$code = <<<ENDOFHTML
case '-#footer#-':
$code = <<<ENDOFHTML
<p>END OF PAGE</p>
case '-#other#-':
$code = <<<ENDOFHTML
<body><p>Here is the body!</p></body>
return $code;
$pagecontent = <<<ENDOFHTML
$pagecontent = preg_replace [php.net]('/(-#[^#]+#-)/e', "codemagic('$1')", $pagecontent);
print $pagecontent;


9:59 am on Jan 25, 2008 (gmt 0)

5+ Year Member

awesome, got it working

I think the problem initially was that there was some object oriented stuff going on. Not sure why this would cause a problem, but it was.


Featured Threads

Hot Threads This Week

Hot Threads This Month