Functions to manage strings in php

Home Magento TopicsFunctions to manage strings in php
mage+php

Functions to manage strings in php

No Comments

Functions to manage strings in php.

In the following post, you can find a small library of functions that can be used for handling and processing of strings in php. It will be useful to create your php modules or extensions in Magento.

Enjoy!

/**
 * functionsStrings.php :  manage & transform strings.
 */
 
/**
 * We check if is activated <strong>get_magic_quotes_gpc</strong>, in the web server.  
 * If it is disabled, is applied  <strong>addslashes</strong> to double the single quote 
 *
 * @param string $inString: string to manage
 * @return string, string managed if it is necessary
 */ 
public function chkstring($inString)
{
    if(!get_magic_quotes_gpc())
    {
        $inString = addslashes($inString);
    }
	return $inString;
}
 
 
/**
 * Change 'not allowed' characters in linux (apostrophes, spaces, diuresis, accents, etc...)
 * using others 'allowed' that gives no problems.  
 *
 * @param string $inString
 * @return string
 */
public function cleanstring($inString)
{
	$arrFrom = array (' ','\'','\\',chr(96),chr(180),chr(241),chr(209),':',chr(225),chr(224),chr(193),chr(192),chr(233),chr(232),chr(201),chr(200),chr(237),chr(236),chr(239),chr(205),chr(204),chr(207),chr(243),chr(242),chr(211),chr(210),chr(250),chr(249),chr(252),chr(218),chr(217),chr(220),'&amp;');
	foreach($arrFrom as $key=&gt;$sTmp) 
        {
		$arrFrom[$key]=utf8_encode($arrFrom[$key]);
	}	
	$arrTo = array ('_','','','','','n','N','','a','a','A','A','e','e','E','E','i','i','i','i','i','i','o','o','O','O','u','u','u','u','u','u','_');
	$inString = str_replace($arrFrom, $arrFrom, $inString) ;	
	return $inString;
}
 
 
/**
* Returns the position of substring included in other string according to concurrence number.
*
* @param string $fromString - String that contains substring to find.
* @param string $findString - String to find.
* @param integer $numConcurrence - Number of  concurrence to find.
* @param integer $iInit - Starting position of search.
* @return integer $iPosition - Position of substring.
*/
function strposbyconcurrence ($fromString, $findString, $numConcurrence, $iInit = 0)
{
   $intStrPos = strpos($fromString, $findString, $iInit);
   if ($intStrPos &gt; 0 &amp;&amp; $numConcurrence &gt; 1)
      $intStrPos = strposbyconcurrence ($fromString, $findString, $numConcurrence-1, $intStrPos+1);
   return $intStrPos;
}
 
 
/**
* Find array values into a string.
*
* @param string $strFieldName
* @param array $arrFieldNameTable
* @return boolean $boolRes
*/
function findInString($strFieldName, $arrFieldNameTable)
{
   $boolRes = false;
   while(list($key, $val) = each ($arrFieldNameTable))
   {
      if (strstr(strtolower($strFieldName), strtolower($val)))
      {
         $boolRes = true;
         break;
      }
   }
   return $boolRes;
}
 
 
/**
* Insert blanks according to indentation number.
*
* @param integer $numBlank
* @return string $txtIndent
*/
function indent($numBlank)
{
   $txtIndent = "";
   $charIns = "&nbsp;";
   for ($i = 0;$i &lt;= $numBlank;$i++)
   {
      $txtIdent = $txtIndent.$charIns;
   }
   return $txtIndent;
}
 
 
/**
 * Check email address syntax.
 *
 * @param string $mailaddress
 * @return boolean
 */
function chkmailaddress($mailaddress)
{ 
	$mailok = true;
	if (!ereg('^([a-zA-Z0-9._]+)@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})
, $mailaddress))
  	{ 
  		$mailok = false; 
  	} 
	return $mailok;
}

Leave a Reply

Your email address will not be published. Required fields are marked *