10 Easy Solutions for PHP String Manipulation - Page 2

6. Converting URLs into Hyperlinks

Many WYSIWYG editors provide toolbars, which allow users to markup text to include hyperlinks. However, you can easily automate this process when the content is rendered to the page, saving your contributors an inconvenient and occasionally error-prone additional step. To convert URLs into hyperlinks, you use the preg_replace() function, which can search a string according to a regular expression that defines the structure of a URL and replace all instances with updated text containing a hyperlinked URL:

7. Removing HTML Tags from a String

Ensuring that user input does not contain potentially hazardous characters, which could lead to SQL injection or cross-site scripting attacks, is one of the primary roles you play as a Web developer. The PHP language includes numerous security-related features that can help you scrub data, including the Filter extension. You might however wish to exercise a bit of additional control over user input rather than taking a "scorched earth" approach. For instance, you could allow users to include some basic HTML within their comments such as a tag. To do this, check out the strip_tags() function, which by default removes all HTML tags from a string, but also allows you to override the default and specify certain allowable tags. For instance, you can strip all tags except for the and using the following statement:

$text = strip_tags($input, "
");

8. Comparing Two Strings

Comparing two strings to ensure they are identical, such as a user's provided password and that user's confirmed password, is a common task. You can do so easily using the substr_compare() function:

If you'd like to compare two strings using a case-insensitive approach, see the function strcasecmp().

9. Converting Newline Characters to Break Tags

Earlier in this article I demonstrated how to save your users the hassle of explicitly creating hyperlinks by using the preg_replace() function to automatically convert URLs into hyperlinks. Continuing with that theme, you can automatically convert any newline characters into HTML tags using the nl2br() function:

$comment = nl2br($comment);

10. Applying Wordwrap

For formatting reasons many word processors, code editors, and websites wrap lines of text that surpass a certain character limit. You can carry out this task using PHP's wordwrap() function:

$speech = "Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."; echo wordwrap($speech, 30);

Executing this snippet produces the following output:

Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.

Conclusion

The functions highlighted here represent only a small portion of those available within PHP's string parsing library. Check out the PHP documentation for a complete list.