Currently Browsing: Functions

strcmp – String Compare

Here are some of string comparation php functions:

  • strcmp($s1, $s2) – returns 0 if $s1 and $s2 are equal, a negativ value (-1) if $s1< $s2 and positive value (1) if $s1>$s2. The string comparation is case-sensitive, character by character, comparing character’s codes. Uppercase chars are considered smaller than minuscule chars. Numbers are considered smaller than letters.
  • strcasecmp($s1, $s2) – idem, but the string compare is case-insensitive
  • strncmp($s1, $s2, $nr) – like strcmp() but it compare only the first $nr chars of the 2 strings.
  • strncasecmp($s1, $s2, $nr) – idem, case-insensitive
  • strnatcmp($s1, $s2) – natural comparation, case-sensitive of the 2 strings (like natsort())
  • strnatcasecmp($s1, $s2) – idem, but case-insensitive

(more…)

strpos – Find the Position of a String

Finding the position of a string or substring can be made with this php functions:

  • strpos($string, $seached_string [,$offset]) – returns position of the first occurrence of $searched_string inside $string or FALSE if $searched_string is not found. If $offset is specified, the seach will be made starting with $offset position.
  • stripos($string, $seached_string [,$offset]) – idem, but the search is case-insensitive
  • strrpos($string, $seached_string [,$offset]) – returns the position of the last occurrence of $searched_string inside $string, or FALSE
  • strripos($string, $seached_string [,$offset]) – idem with strrpos but is case-insensitive

(more…)

PHP sef url function

This is another PHP sef function, much better to use in case you want to get remove characters that are not a-z, 0-9, dash, underscore or space, then remove all leading and trailing spaces, change all dashes, underscores and spaces to dashes and return the modified string in lower case.
(more…)

PHP substr() function

PHP substr() function will substract or returns the portion of string specified by the start and length parameters.
substr($string , int $start [, int $length ] )

  1. <?php
  2. $sub = substr("abcdef", -2);    // returns "ef", -2 represent starting from 2 character back
  3. $sub = substr("abcdef", -3, 1); // returns "d", -3 represent starting from 3 character back and 1 the length of the substraction
  4. echo substr('abcdef', 0, 3);  // returns "abc", starting from 0 to 3
  5.  
  6. // you can substract only one character
  7. $string = 'abcdef';
  8. echo $string[3]; // returns "d", the third character
  9. ?>
Page 1 of 212