Page 1 of 1
php advice..
Posted: Mon Dec 19, 2005 4:44 pm
by Turbanator
Say I have a string of txt, $text. And I want to read through the text and find all occurances of the string '[cheese]*[/cheese]' where * represents a number. Then with each occurance i want to replace the occurance with a look up performed by cheesegrater(*) where * is the number from [cheese]*[/cheese], how would I do this?
For example:
cheesegrater(1); returns "cheddar"
so upon performing the above command on the following string:
"raj loves [cheese]1[/cheese]"
the code would look up cheesegrater(1); and replace [cheese]1[/cheese] with cheddar, to return:
"raj loves cheddar"
Any ideas?
Posted: Mon Dec 19, 2005 5:24 pm
by 4days
can't you just regex it, or does it need to be done frequently over large strings?
and do you mean you want a function that handles $text and calls another function called cheesegrater?
is the cheesegrater function going to be doing something else that necessitates it being a function in it's own right?
guessing you know what the potential values in the delimters could be, so if they're numbers then why not just use the number to point to the right place in an array?
so far it sounds like something you could hack a bbcode tutorial to do.
Posted: Mon Dec 19, 2005 9:26 pm
by PhoeniX
Code: Select all
<?
// The message to replace things in
$str = "
I like [cheese]1[/cheese] and [cheese]2[/cheese],
[cheese]3[/cheese] is quite nice though at times. My favourite has to be
[cheese]4[/cheese] though!
";
// Convert all applicable characters to HTML entities (php.net)
$str = htmlentities($str);
// The code to search for, in this case
// [cheese]*[/cheese]
//
// Note: /e means it'll call a function, rather than just displaying
// the cheesegrater($1) text
$cheesy_search = array (
'/\[cheese\](.*?)\[\/cheese\]/e',
);
// What we are replacing [cheese]*[/cheese] with
// as we have /e this calls the function
$replace = array(
'cheesegrater($1)',
);
// Replace
$str = preg_replace ($cheesy_search, $replace, $str);
// Output
echo $str;
// Takes $cheeseID and returns cheese (return needed for preg_replace)
function cheesegrater( $cheeseID )
{
switch ( $cheeseID )
{
case 1:
return "cheddar";
break;
case 2:
return "red leicester";
break;
case 3:
return "mexicana";
break;
case 4:
return "five counties";
break;
default:
return "cheddar";
break;
}
}
?>
This should work. I commented it so it should make sence.
Posted: Tue Dec 20, 2005 1:01 am
by Turbanator
thanks man, worked a charm
here was what i was actually using it for:
Code: Select all
$article_search = array ('/\[article\](.*?)\[\/article\]/e',);
$replace = array('format_enms_article($1)',);
$enms_mail_text = preg_replace ($article_search, $replace, $enms_mail_text);
$enms_mail_text=($enms_mail_text);
it's an article management system for an e-newsletter app.
Posted: Tue Dec 20, 2005 2:32 am
by PhoeniX
Nice

.
Obviously you an add more items to the array lists of both search/replace to replace more than one thing at once. You can probably put the search / replace [article] things straight into the preg_replace function though I've not tried it.
Least itworks anyway.