rain13 Posted July 18, 2011 Share Posted July 18, 2011 I was unable to find some funcs so I decided to write those myself. just wanted to know what do you think of these. Do you think they're useful?StringLeft - Returns a number of characters from the left-hand side of a string.StringRight - Returns a number of characters from the right-hand side of a string.StringTrimLeft - Trims a number of characters from the left hand side of a string.StringTrimRight - Trims a number of characters from the right hand side of a string. function StringLeft($sString,$iCount){ return substr ( $sString ,0, $iCount );}function StringRight($sString,$iCount){ return substr ( $sString , strlen($sString) - $iCount, strlen($sString));}function StringTrimLeft($sString,$iCount){ return substr ( $sString ,$iCount, strlen($sString) );}function StringTrimRight($sString,$iCount){ return substr ( $sString ,0 , strlen($sString) - $iCount);} example: $str = "Hello World";echo StringLeft($str,1)."<br>";echo StringRight($str,1)."<br>";echo StringTrimLeft($str,1)."<br>";echo StringTrimRight($str,1)."<br>"; output of example: Hdello WorldHello WorlEdit: some of these are at http://php.net/manual/en/function.substr.php but hey are like 10 lines long and look terrible, my funcs are all single line. Link to comment Share on other sites More sharing options...
jeffman Posted July 18, 2011 Share Posted July 18, 2011 It is common to create very short functions that explicitly name what they do. It saves you or other developers the trouble of figuring out what a particular call to something like substr is trying to accomplish.It might be worth your time to play around with negative values for the length or index arguments when you're messing around with the right side of a string.Also note situations where it is not necessary to pass a length to substr. If length is omitted, the substring starting from start until the end of the string will be returned. Link to comment Share on other sites More sharing options...
rain13 Posted July 19, 2011 Author Share Posted July 19, 2011 If you can think of any other func that would be useful you could let me know. Link to comment Share on other sites More sharing options...
jeffman Posted July 19, 2011 Share Posted July 19, 2011 That's hard. PHP has SO MANY functions for strings and arrays. Link to comment Share on other sites More sharing options...
ApocalypeX Posted July 19, 2011 Share Posted July 19, 2011 Why don't you add them to the string object instead of random functions?String.prototype.methodName = function(){....} Link to comment Share on other sites More sharing options...
jeffman Posted July 19, 2011 Share Posted July 19, 2011 SoItBegins is working in PHP. If it had been JavaScript, I'd have made a dozen suggestions (basically, prototyping the kinds of functions PHP already provides). Link to comment Share on other sites More sharing options...
rain13 Posted July 20, 2011 Author Share Posted July 20, 2011 prototyping the kinds of functions PHP already providesDoes this mean i can co something like his in php?$var = abc;$var.StringRight(this,1) Link to comment Share on other sites More sharing options...
thescientist Posted July 20, 2011 Share Posted July 20, 2011 Why don't you add them to the string object instead of random functions?String.prototype.methodName = function(){....}wrong language... Link to comment Share on other sites More sharing options...
jeffman Posted July 20, 2011 Share Posted July 20, 2011 @SoItBeginsDefinitely not. Not only is the syntax wrong (you'd use the -> operator, not the dot) but PHP doesn't let you create/tweak objects on the fly like that. You'd need to write a class, then create an instance of the class.And it would never work the way a built-in JavaScript object works. Strings and array in PHP are primitive data types, not objects in their own right. There is no circumstance in which you can add properties or methods to a PHP string or array. The best you can do is create an object that has a string or array as a property. Eventually, that would let do things like this:$instance = new MyObject ();$instance->data = "abc";$instance->doSomething();(And remember, I can only do that if the script also contains a definition for the MyObject class.) Link to comment Share on other sites More sharing options...
thescientist Posted July 20, 2011 Share Posted July 20, 2011 and of course if those properties aren't private. It was clear the most saddening part of my PHP learning was that I couldn't create array/object literals on the fly. Oh, it nearly drove me to tears! :)But now I embrace it, because in a sense the strictness of it enforces a good formality with common and more widely used classical coding conventions. Although I still do love JS syntax and language constructs a whole lot more, I've used it to try and add more structure to my javascripts. Link to comment Share on other sites More sharing options...
trinistorm Posted July 24, 2011 Share Posted July 24, 2011 and of course if those properties aren't private. It was clear the most saddening part of my PHP learning was that I couldn't create array/object literals on the fly. Oh, it nearly drove me to tears! :)But now I embrace it, because in a sense the strictness of it enforces a good formality with common and more widely used classical coding conventions. Although I still do love JS syntax and language constructs a whole lot more, I've used it to try and add more structure to my javascripts.Javascript really is fun to use with all that Object construction and weak typing etc, but for someone first stepping into the programming realm it can give you some seriously bad habits, like assuming Strings are objects. By trying out different languages you get to see how languages are organized differently and I have a greater appreciation for all I come across even Pascal. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.