homepage Welcome to WebmasterWorld Guest from 67.202.56.112
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
A function with ereg to find data between particular characters
el_roboto




msg:3556844
 1:58 pm on Jan 24, 2008 (gmt 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

 

el_roboto




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

I've tried

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

but I get a blank output, any ideas?

coopster




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

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;

el_roboto




msg:3557700
 9:59 am on Jan 25, 2008 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved