Welcome to WebmasterWorld Guest from 54.196.74.153

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)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


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)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


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)

Administrator

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

joined:July 31, 2003
posts:12548
votes: 2


With the PCRE [php.net] you could do something like this:
function codemagic($code) 
{
switch ($code) {
case '-#header#-':
$code = <<<ENDOFHTML
<head><title>Title</title></head>
ENDOFHTML;
break;
case '-#footer#-':
$code = <<<ENDOFHTML
<p>END OF PAGE</p>
ENDOFHTML;
break;
case '-#other#-':
$code = <<<ENDOFHTML
<body><p>Here is the body!</p></body>
ENDOFHTML;
break;
}
return $code;
}
$pagecontent = <<<ENDOFHTML
<html>
-#header#-
-#other#-
-#footer#-
</html>
ENDOFHTML;
$pagecontent = preg_replace [php.net]('/(-#[^#]+#-)/e', "codemagic('$1')", $pagecontent);
print $pagecontent;
9:59 am on Jan 25, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 14, 2007
posts:92
votes: 0


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.