www.webdeveloper.com
Results 1 to 3 of 3

Thread: Extracting Text within [] in TinyMCE Textarea

  1. #1
    Join Date
    Jul 2012
    Posts
    14

    Exclamation Extracting Text within [] in TinyMCE Textarea

    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].

    Can anyone help? I appreciate it!!!

  2. #2
    Join Date
    Jul 2012
    Posts
    14
    Hi everyone!

    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!

  3. #3
    Join Date
    May 2003
    Posts
    598
    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]);

         while(
    substr_count($content,'[') > 0){
                  
    $replace_key get_string_between($content,'[',']');
                  
    $replace_val $row[$replace_key];
                  
    $content str_replace('['.$replace_key.']',$replace_val,$content);
         } 
    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.
    We do precision guesswork

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles