I really need help. I have been researching this, and all I've accomplished is confusing myself even more!
I have a page that allows the user to edit an email. The text within the TinyMCE textarea is saved to the database. I need it to be able to use shortcuts such as [ShipDate] and [TrackingNumber], and then when the user clicks "Send Email", I will extract every one of these and replace it with a value from the database.
I am trying to figure out how to integrate preg_replace to accomplish this, but I'm confused by all the symbols that follow it, so I have no clue as to how I would signify the different types ([ShipDate] vs [TrackingNumber], etc.) I'm also not sure how to make sure it gets ALL of the "variables" from the text.
I also tried to figure out if there was a way to add a shortcode button to TinyMCE that would allow the user to simply click a button to insert the ship date, then process it when the email was sent, but didn't have any luck there either.
Can anyone help me figure out the best way to do this? An email may look like this:
Your order has shipped on [ShipDate] by [ShippingMethod]. Your tracking number is [TrackingNumber]. To track your order, go to [TrackingURL].
I don't know why I didn't think of this before...probably just overthinking it (I do that often)...
Anyway, I can do this using str_replace(). So I guess my only question now is if someone can help me with creating buttons on TinyMCE that when clicked, will add the appropriate "[string]" in the textarea. Everything I look up is for Wordpress! Thanks!
You could use str_replace() if you wanted to maintain a static map of characters / db values to replace...but the way to make it dynamic (so that you would never have to edit the code) and make it more plug and play, to work with multiple form / database combinations is like this:
PHP Code:
/**
* get_string_between()
* @desc extract text between two strings (or chars)...useful for templates, etc.
* @param string $str
* @param string $start
* @param string $end
* @return string
*/
function get_string_between($str, $start, $end){
$str = " ".$str;
$ini = strpos($str,$start);
if ($ini == 0){
return "";
}
$ini += strlen($start);
$len = strpos($str,$end,$ini) - $ini;
return substr($str,$ini,$len);
}
$content = $_POST['TinyMCE_Area'];
//......
//db qry setup
//......
$result = mysql_query("SELECT * FROM Persons WHERE id=131");
$row = mysql_fetch_array($result[0]);
This will dynamically extract the text between the brackets, and replace it with the database value matching the pattern...allowing you to add new template buttons on the fly.
Bookmarks