PHP Classes

PHP String Foreach: Process strings as arrays of characters

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 150 All time: 9,087 This week: 673Up
Version License PHP version Categories
php-string-foreach 2.0.0Custom (specified...5PHP 5, Text processing
Description 

Author

This class can process strings as arrays of characters.

It provides functions to manipulate given strings that are equivalent to those for manipulating arrays.

Currently it can provides functions like explode, foreach, strposall, strposreplace, strposadd, etc..

Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse <contact>
Classes: 50 packages by
Country: Benin Benin
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Recommendations

Convert Ms Word to plaint text and count number of words
How to count word from plaintext

Get all words in sentence
Some class to get all of words in sentence

What is the best PHP count words class?
Count the number of words in a string

Example

<?php
require_once('./chars.class.php');

echo
'<pre>';
$exemple=new Chars('I want to get all of words from this sentence.

After having owned the BMW X3, the MDX is a pleasure to drive. Reliability is great for 25,000 miles. Only things wrong are Bluetooth; rings and cannot hear whose on the other end and many viable addresses do not show up on Navigation system. Dealer service excellent. A few miss-steps in design; no lights for A/C controls at night; rear view pic doesn\'t come on in reverse for 10 seconds, some wind noise, transmission can be rough and erratic. No ipod plug which I didn\'t realize until after I purchased. Front end design could be improved, very masculine. Sounds like a lot but I do really like this car. Also test drove BMW X5 and Lexus 400.'
);
print_r($exemple->explode('\word'));
echo
nl2br($exemple->wordwrap());
echo
'<h2>Find all the phrases in a given string</h2>';
print_r($exemple->explode('#\W#u'));
       
       
$Chars= new Chars (' hail mary 2pac amaru shakur >> 1');
echo
'<pre>';
echo
$Chars->trim()->sub_stri_position_add('a','po',3,false)->stri_position_replace('A','',2)->delete_last()->delete_first()->str_position_toupper('a',2)->str_position_add('a',3);

$exemple=new Chars('Nbvjjjjjv .hhhwjjjjjdhjjjjd. ghjaudUUyyUUggue. ghjauduuyyuuggue.ghjauduuyyuuggue.
ghjauduuyyuuggue.
ghjauduuyyuuggue. ghuuuuu'
);
echo
$exemple->upper_sentence_start()->data()->count_UnicodeChars();

var_dump(preg_match('#^N#i',$exemple));
$auth =chars("24\tLewis Carroll");
$n = sscanf($auth, "%d\t%s %s", $id, $first, $last);
echo
"<author id='$id'>
    <firstname>
$first</firstname>
    <surname>
$last</surname>
</author>\n"
;
echo
'<pre>';
?>


Details

This package contains a class which allows to handle strings as arrays of characters while keeping the ability to be treated as a simple string,it is a useful way to handle some actions over the string type of PHP. It allows user to look and go trough a string just like an array and then make easier manipulations like search, replacement, deletion,explosion,case transformation. Most of the implemented methods are chain-able. Example: $Chars= new Chars (' hail mary 2pac amaru shakur >> 1'); echo $Chars->trim() ->sub_stri_position_add('a','po',3,false) ->stri_position_replace('A','',2) ->delete_last() ->delete_first() ->str_position_toupper('a',2) ->str_position_add('a',3); Although the fact that most of common functions are implemented as methods this class is specially useful to chain operations or to access the string as arrays or the best part, use foreach loop on the string easily.You can avoid method and directly use common functions waiting strings as parameters on the object just as a simple PHP string. Examples : preg_match('#shakur#',$Chars); $auth =chars("24\tLewis Carroll"); $n = sscanf($auth, "%d\t%s %s", $id, $first, $last); echo "<author id='$id'> <firstname>$first</firstname> <surname>$last</surname> </author>\n"; The class handle multibytes strings too. Class [ class Chars implements Countable, Iterator, Traversable, ArrayAccess, JsonSerializable ] { - Constants [0] { } - Static properties [0] { } - Properties [1] { Property [ private $chars=array() ] } - Static methods [6] { private static allnumberdetect(Parameter #0 [ <required> $str ]) private static isPcrePattern(Parameter #0 [ <required> $pattern ]) private static numberdetect(Parameter #0 [ <required> $str ]) protected static split(Parameter #0 [ <required> $string ]) public static __callStatic(Parameter #0 [ <required> $name ],Parameter #1 [ <required> $arguments ]) public static __set_state(Parameter #0 [ <required> $array ]) } - Methods [118] { public addcslashes() public addslashes() public bin2hex() public concat(Parameter #0 [ <required> $string ],Parameter #1 [ <optional> $side = 1 ]) public convert_cyr_string() public convert_uudecode() public convert_uuencode() public count() public count_chars(Parameter #0 [ <optional> $mode = 0 ]) public count_UnicodeChars(Parameter #0 [ <optional> $mode = 0 ]) public crc32(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> &$crc32 = NULL ]) public crypt(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> $salt = '' ],Parameter #2 [ <optional> &$crypt = NULL ]) public current() public c_rot(Parameter #0 [ <required> $r ]) public delete_first() public delete_last() public end() public explode(Parameter #0 [ <optional> $separate = NULL ]) public fprintf(Parameter #0 [ <required> $handle ]) public get() public getAndChain(Parameter #0 [ <required> &$chars ]) public hebrev() public hebrevc() public hex2bin() public htmlspecialchars() public htmlspecialchars_decode() public html_entities() public html_entity_decode() public intervertTwoCharsPos() public jsonSerialize() public key() public lcfirst() public LiftUpToPush(Parameter #0 [ <required> $value ]) public lower_pos(Parameter #0 [ <required> $pos ]) public ltrim(Parameter #0 [ <optional> $charlist = " \t\n\r\0\x0B" ]) public md5(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> &$md5 = NULL ]) public metaphone(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> $phone = 0 ],Parameter #2 [ <optional> &$metaphone = NULL ]) public money_format(Parameter #0 [ <required> $number ]) public next() public nl2br() public number_format(Parameter #0 [ <optional> $decimals = 0 ],Parameter #1 [ <optional> $dec_point = '.' ],Parameter #2 [ <optional> $thousands_sep = ',' ]) public offsetExists(Parameter #0 [ <required> $offset ]) public offsetGet(Parameter #0 [ <required> $offset ]) public offsetSet(Parameter #0 [ <required> $offset ],Parameter #1 [ <required> $value ]) public offsetUnset(Parameter #0 [ <required> $offset ]) public preg_replace(Parameter #0 [ <required> $pattern ],Parameter #1 [ <required> $replacement ],Parameter #2 [ <required> $limit ],Parameter #3 [ <required> &$count ]) public prev() public printf() public quoted_printable_decode() public quoted_printable_encode() public quotemeta() public rewind() public rtrim(Parameter #0 [ <optional> $charlist = " \t\n\r\0\x0B" ]) public sha1(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> &$sha1 = NULL ]) public show(Parameter #0 [ <optional> $concat = '' ]) public soundex(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> &$soundex = NULL ]) public sprintf() public strcspn(Parameter #0 [ <required> $mask ],Parameter #1 [ <optional> $start = 0 ],Parameter #2 [ <optional> $length = NULL ]) public stripcslashes() public stripos(Parameter #0 [ <required> $str ],Parameter #1 [ <optional> &$stripos = false ]) public stripslashes() public strip_tags(Parameter #0 [ <optional> $tags = '' ]) public stristr(Parameter #0 [ <required> $needle ],Parameter #1 [ <optional> $beforeneedle = false ]) public stri_position_all(Parameter #0 [ <optional> $str = ' ' ],Parameter #1 [ <optional> &$find = false ]) public stri_position_replace(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $s2 ],Parameter #2 [ <required> $pos ]) public stri_position_tolower(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $pos ]) public stri_position_toupper(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $pos ]) public strlen() public strpbrk(Parameter #0 [ <required> $charlist ]) public strpos(Parameter #0 [ <required> $str ],Parameter #1 [ <optional> &$strpos = false ]) public strrchr(Parameter #0 [ <required> $needle ]) public strrev() public strripos(Parameter #0 [ <required> $str ],Parameter #1 [ <optional> &$strripos = false ]) public strrpos(Parameter #0 [ <required> $str ],Parameter #1 [ <optional> &$strrpos = false ]) public strspn(Parameter #0 [ <required> $mask ],Parameter #1 [ <optional> $start = 0 ],Parameter #2 [ <optional> $length = NULL ]) public strstr(Parameter #0 [ <required> $needle ],Parameter #1 [ <optional> $beforeneedle = false ]) public strtolower() public strtoupper() public strtr() public str_getcsv(Parameter #0 [ <required> &$csv ],Parameter #1 [ <optional> $delimiter = ',' ],Parameter #2 [ <optional> $enclosure = '"' ],Parameter #3 [ <optional> $escape = '\' ]) public str_ireplace(Parameter #0 [ <required> $needle ],Parameter #1 [ <required> $replace ],Parameter #2 [ <optional> &$count = false ]) public str_pad(Parameter #0 [ <required> $pad_length ],Parameter #1 [ <required> $pad_string ],Parameter #2 [ <optional> $pad_type = 1 ]) public str_position_add(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $pos ],Parameter #2 [ <optional> $before = true ]) public str_position_all(Parameter #0 [ <optional> $str = ' ' ],Parameter #1 [ <optional> &$find = false ]) public str_position_replace(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $s2 ],Parameter #2 [ <required> $pos ]) public str_position_tolower(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $pos ]) public str_position_toupper(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $pos ]) public str_repeat(Parameter #0 [ <required> $multiplier ],Parameter #1 [ <optional> $mutate = true ],Parameter #2 [ <optional> &$result = '' ]) public str_replace(Parameter #0 [ <required> $needle ],Parameter #1 [ <required> $replace ],Parameter #2 [ <optional> &$count = false ]) public str_rot13(Parameter #0 [ <optional> $mutate = true ],Parameter #1 [ <optional> &$str_rot13 = NULL ]) public str_shuffle() public substr(Parameter #0 [ <required> $start ],Parameter #1 [ <optional> $length = NULL ]) public substr_compare(Parameter #0 [ <required> $string ],Parameter #1 [ <optional> $offset = 0 ],Parameter #2 [ <optional> $length = NULL ],Parameter #3 [ <optional> $case_insensitive = false ]) public substr_count(Parameter #0 [ <required> $needle ],Parameter #1 [ <optional> $offset = 0 ],Parameter #2 [ <optional> $length = NULL ]) public substr_replace(Parameter #0 [ <required> $replacement ],Parameter #1 [ <optional> $start = 0 ],Parameter #2 [ <optional> $length = NULL ]) public sub_stri_position_add(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $s2 ],Parameter #2 [ <required> $pos ],Parameter #3 [ <optional> $before = true ]) public sub_stri_tolower(Parameter #0 [ <required> $str ]) public sub_stri_toupper(Parameter #0 [ <required> $str ]) public sub_str_position_add(Parameter #0 [ <required> $s ],Parameter #1 [ <required> $s2 ],Parameter #2 [ <required> $pos ],Parameter #3 [ <optional> $before = true ]) public sub_str_tolower(Parameter #0 [ <required> $str ]) public sub_str_toupper(Parameter #0 [ <required> $str ]) public trim(Parameter #0 [ <optional> $charlist = " \t\n\r\0\x0B" ]) public ucfirst() public ucwords() public upper_sentence_start() public upper_string_position(Parameter #0 [ <required> $pos ]) public valid() public vfprintf(Parameter #0 [ <required> $handle ],Parameter #1 [ <required> $arguments ]) public vprintf(Parameter #0 [ <required> $arguments ]) public vsprintf(Parameter #0 [ <required> $arguments ]) public wordwrap(Parameter #0 [ <optional> $width = 75 ],Parameter #1 [ <optional> $break = '\n' ],Parameter #2 [ <optional> $cut = false ]) public __call(Parameter #0 [ <required> $name ],Parameter #1 [ <required> $arguments ]) public __construct(Parameter #0 [ <required> $string ]) public __debugInfo() public __get(Parameter #0 [ <required> $name ]) public __invoke(Parameter #0 [ <required> $arguments ]) public __set(Parameter #0 [ <required> $name ],Parameter #1 [ <required> $value ]) public __toString() } }

  Files folder image Files (4)  
File Role Description
Plain text file chars.class.php Class PHP String foreach class source
Accessible without login Plain text file license Lic. license
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file teststrforeach.php Example example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:150
This week:0
All time:9,087
This week:673Up