The StringValidator library is a powerful tool for validating and manipulating strings. It provides a wide range of functions that can be used to check the validity of input strings, sanitize user inputs, and perform various string operations. With its intuitive interface and comprehensive features, StringValidator is an essential tool for developers working with string data.
Features
- Validation of string length
- Checking if a string is empty or not
- Verification of email addresses
- Validation of URLs
- Checking if a string contains only alphabets or digits
- Verification of phone numbers
- Sanitizing and removing HTML tags from strings
- Counting the number of words in a string
- Converting strings to uppercase or lowercase
- Determining if a string starts or ends with a specific substring
- Truncating long strings
Installation
To use StringValidator in your project, simply follow these steps:
- Open your terminal or command prompt.
- Navigate to your project directory.
- Run the command
composer require cocoadocs/stringvalidator
. - The library will be downloaded and added to your project’s dependencies.
- Include the autoload file in your project to start using StringValidator.
Usage
Using StringValidator is straightforward. You can start by creating an instance of the StringValidator
class:
$validator = new StringValidator();
From there, you can use the various methods provided by the library to validate or manipulate your strings. Here are some examples:
// Validate string length
$validator->validateLength('Hello', 2, 10); // Returns true
// Check if a string is empty
$validator->isEmpty(''); // Returns true
// Verify email address
$validator->validateEmail('name@example.com'); // Returns true
// Validate URL
$validator->validateUrl('http://example.com'); // Returns true
// Check if a string contains only alphabets or digits
$validator->isAlphaNumeric('Alpha123'); // Returns true
// Verify phone number
$validator->validatePhoneNumber('+1234567890'); // Returns true
// Sanitize and remove HTML tags from string
$validator->sanitizeString('Hello <strong>World</strong>
'); // Returns 'Hello World'
// Count number of words in string
$validator->countWords('This is a sample sentence.'); // Returns 5
// Convert string to uppercase
$validator->toUppercase('hello'); // Returns 'HELLO'
// Convert string to lowercase
$validator->toLowercase('WORLD'); // Returns 'world'
// Check if a string starts with a specific substring
$validator->startsWith('Hello World', 'Hello'); // Returns true
// Check if a string ends with a specific substring
$validator->endsWith('Hello World', 'World'); // Returns true
// Truncate long strings
$validator->truncate('This is a long string', 6); // Returns 'This is...'
Additional Information
StringValidator also provides several additional features that are worth exploring:
- The library supports custom validation rules.
- Advanced string manipulation operations are available.
- Error handling and reporting mechanisms are implemented.
- Unit tests and comprehensive documentation are provided.
Conclusion
The StringValidator library offers an extensive set of features for validating, sanitizing, and manipulating strings in your projects. With its ease of use and wide range of functionalities, it is a valuable tool for developers working with string data. Explore the library and take advantage of its capabilities to enhance your string handling workflows.