How to conditionally hide a space in a rules action if the first token is empty? I am using the tokens module to print personalized email notifications. Sometimes a webform component is not filled out and the email notification leaves an empty space before the token. I am using the Rules module to send a mail.

E.g.

[data:title-value-raw] [data:name-value-raw]


Default output

Mr. Example


Expected result

Example


Actual result

 Example


How can I hide the space if the first token is empty?

Drupal rule to send message.

{ "rules_mail_confirmation" : {
"LABEL" : "Mail Confirmation",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "webform_rules" ],
"ON" : { "webform_rules_submit" : [] },
"IF" : [
  { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
  { "node_is_webform" : { "node" : [ "node" ] } }
],
"DO" : [
  { "mail" : {
      "to" : "[data:email-value]",
      "subject" : "Confirmation",
      "message" : "[data:title-value-raw] [data:name-value-raw],\r\n\r\n",
      "from" : "[site:mail]"
    }
  }
]


}
}
If I was to implement this, I'd create a special (tiny) Rules Component as a kind of helper function, and use that in your existing rule also. The clue of that Rules Component is the (not often used) "Provided" facility for one of its parameters. More details below ...

Part 1: Create a Rules Component

Create a new Rules Component that looks like so (in Rules Export format):

{ "rules_create_email_body_prefix" : {
    "LABEL" : "Create eMail body prefix",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "title_value" : { "label" : "Title value", "type" : "text" },
      "prefix_value" : { "label" : "Prefix value", "type" : "text", "parameter" : false }
    },
    "IF" : [ { "NOT data_is_empty" : { "data" : [ "title-value" ] } } ],
    "DO" : [
      { "data_set" : { "data" : [ "prefix-value" ], "value" : "[title-value:value] " } }
    ],
    "PROVIDES VARIABLES" : [ "prefix_value" ]
  }
}


Some details about the above Rules Component:


It uses 2 parameters:


it accepts title_value as an input parameter (Usage = Parameter).
it provides prefix_value as output of it (Usage = Provided).

The logic of this Rules Component is like so:


Rules Condition: verify that the value of title_value is NOT empty.
Rules Action: set the value of prefix_value equal to the value provided via title_value, and follow it by a space (note!).



Part 2: Integrate the Rules Component in your existing rule

Here is an updated version of your rule (also in rules export format).

{ "rules_mail_confirmation_with_optional_prefix" : {
    "LABEL" : "Mail Confirmation with optional prefix",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "webform_rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "contact" : "contact" } } } },
      { "node_is_webform" : { "node" : [ "node" ] } }
    ],
    "DO" : [
      { "component_rules_create_email_body_prefix" : {
          "USING" : { "title_value" : "[data:title-value-raw]" },
          "PROVIDE" : { "prefix_value" : { "prefix_value" : "Prefix value" } }
        }
      },
      { "mail" : {
          "to" : "[data:email-value]",
          "subject" : "Confirmation",
          "message" : "[prefix-value][data:name-value-raw],\r\n\r\n",
          "from" : "[site:mail]",
          "language" : [ "" ]
        }
      }
    ]
  }
}


These are the changes I applied (as compared to your rule):


Rename the rule (to not override your own rule).
Add a Rules Action (as the first one) to invoke the newly created Rules Component (whereas the value [data:title-value-raw] is passed to the title_value parameter).
Adapt the message body of the eMail, to now use the prefix_value as provided by the Rules Component. To be complete: if the Rules Condition in the Rules Component is NOT satisfied, the value of prefix_value is empty, otherwise the Rules Component will assign a value is equal to title_value followed by a space.


Notes:

The exported rule as included in the 1st revision of your question, contains some errors/challenges, i.e:


It contains a syntax error, the comma at the end of your 2nd Rules Condition should not be there (only after I removed that comma I was able to import your rule ...).
Something doesn't seem right in the [data:[data:title-value-raw]] token you included, pretty sure it should be [data:title-value-raw], as confirmed somehow also in the edit you applied in revision 6 of your question.
IMO there is no need for "PHP" (as shown within "REQUIRES" in your exported rule ...), as confirmed somehow also in the edit you applied in revision 6 of your question.