Merge pull request #5250 from jitendrapurohit/CRM-15934update
[civicrm-core.git] / CRM / Utils / String.php
index 08d2470716f2ffd0c20ac04e194f86cabb29b1ba..b561cad091520ce2d7915a0d803cabe34aff5ade 100644 (file)
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
- *
  */
 
 require_once 'HTML/QuickForm/Rule/Email.php';
 
 /**
- * This class contains string functions
+ * This class contains string functions.
  *
  */
 class CRM_Utils_String {
@@ -48,8 +47,7 @@ class CRM_Utils_String {
   const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
 
   /**
-   * Convert a display name into a potential variable
-   * name that we could use in forms/code
+   * Convert a display name into a potential variable name.
    *
    * @param $title title of the string
    * @param int $maxLength
@@ -70,8 +68,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Given a string, replace all non alpha numeric characters and
-   * spaces with the replacement character
+   * Replace all non alpha numeric characters and spaces with the replacement character.
    *
    * @param string $name
    *   The name to be worked on.
@@ -80,12 +77,11 @@ class CRM_Utils_String {
    * @param int $len
    *   Length of valid variables.
    *
-   *
    * @return string
    *   returns the manipulated string
    */
   public static function munge($name, $char = '_', $len = 63) {
-    // replace all white space and non-alpha numeric with $char
+    // Replace all white space and non-alpha numeric with $char
     // we only use the ascii character set since mysql does not create table names / field names otherwise
     // CRM-11744
     $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
@@ -101,12 +97,11 @@ class CRM_Utils_String {
 
   /**
    * Convert possibly underscore separated words to camel case with special handling for 'UF'
-   * e.g
-   * membership_payment returns MembershipPayment
+   * e.g membership_payment returns MembershipPayment
+   *
    * @param string $string
    *
    * @return string
-   *   string
    */
   public static function convertStringToCamel($string) {
     $fragments = explode('_', $string);
@@ -121,8 +116,7 @@ class CRM_Utils_String {
   }
 
   /**
-   *
-   * Takes a variable name and munges it randomly into another variable name
+   * Takes a variable name and munges it randomly into another variable name.
    *
    * @param string $name
    *   Initial Variable Name.
@@ -139,15 +133,16 @@ class CRM_Utils_String {
 
   /**
    * Takes a string and returns the last tuple of the string.
-   * useful while converting file names to class names etc
+   *
+   * Useful while converting file names to class names etc
    *
    * @param string $string
    *   The input string.
-   * @param \char|string $char $char the character used to demarcate the componets
-   *
+   * @param string $char
+   *   Character used to demarcate the components
    *
    * @return string
-   *   the last component
+   *   The last component
    */
   public static function getClassName($string, $char = '_') {
     $names = array();
@@ -160,8 +155,9 @@ class CRM_Utils_String {
   }
 
   /**
-   * Appends a name to a string and seperated by delimiter.
-   * does the right thing for an empty string
+   * Appends a name to a string and separated by delimiter.
+   *
+   * Does the right thing for an empty string
    *
    * @param string $str
    *   The string to be appended to.
@@ -169,8 +165,6 @@ class CRM_Utils_String {
    *   The delimiter to use.
    * @param mixed $name
    *   The string (or array of strings) to append.
-   *
-   * @return void
    */
   public static function append(&$str, $delim, $name) {
     if (empty($name)) {
@@ -201,7 +195,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Determine if the string is composed only of ascii characters
+   * Determine if the string is composed only of ascii characters.
    *
    * @param string $str
    *   Input string.
@@ -244,7 +238,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Determine the string replacements for redaction
+   * Determine the string replacements for redaction.
    * on the basis of the regular expressions
    *
    * @param string $str
@@ -360,13 +354,13 @@ class CRM_Utils_String {
   }
 
   /**
-   * Extract variable values
+   * Extract the civicrm path from the url.
    *
-   * @param mix $query
-   *   This is basically url.
+   * @param string $query
+   *   A url string.
    *
-   * @return mix
-   *   $v  returns civicrm url (eg: civicrm/contact/search/...)
+   * @return string|null
+   *   civicrm url (eg: civicrm/contact/search)
    */
   public static function extractURLVarValue($query) {
     $config = CRM_Core_Config::singleton();
@@ -662,7 +656,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * Generate a random string
+   * Generate a random string.
    *
    * @param $len
    * @param $alphabet
@@ -727,7 +721,7 @@ class CRM_Utils_String {
   }
 
   /**
-   * This function compares two strings
+   * This function compares two strings.
    *
    * @param string $strOne
    *   String one.
@@ -779,4 +773,19 @@ class CRM_Utils_String {
     return str_replace('&', '&', $htmlUrl);
   }
 
+  /**
+   * Formats a string of attributes for insertion in an html tag.
+   *
+   * @param array $attributes
+   *
+   * @return string
+   */
+  public static function htmlAttributes($attributes) {
+    $output = '';
+    foreach ($attributes as $name => $vals) {
+      $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"';
+    }
+    return ltrim($output);
+  }
+
 }