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?
php advice..
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.
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.
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.
-
Turbanator
- Posts: 883
- Joined: Wed Jun 08, 1983 7:00 am
thanks man, worked a charm
here was what i was actually using it for:
it's an article management system for an e-newsletter app.
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);