Search string in PHP string: description of features

PHP suggests using various options for finding strings in other strings. There are the usual features. It is possible to use regular expressions. The developer can develop his own version of the search for information subject to the rules of syntax.

search string in php string

String search is not limited to confirming the presence of the desired character or string in the right place. An object can be a string - this is a more capacious information concept than just a string. The search is especially effective when the moment of detection is accompanied by an immediate decision, and does not require additional processing.

Key Search Functionality

Finding a string in a PHP string is the strpos () function. Its result is either a Boolean value, or the position of the search term (second parameter) in the string (first parameter) from the position (third parameter). If the last parameter is not specified, the search is performed from the zero position of the string.

search string in php string

In this example, a PHP string search is shown with a single character. Instead of a character, you can use a string. PHP does not limit the programmer to string sizes, but reasonable limits are essential.

You can search for strings in strings, case sensitive, from the first or last occurrence, and use regular expressions. As in all other cases, the encoding of the page on which the script is located is essential.

In some cases, it is advisable to use the option iconv_strpos (), rather than just strpos (). In the vast majority of cases, searching for a substring in a PHP string will provide perfectly accurate: just keep the encoding of the page and the string in the same encoding.

Features of the standard approach

If the search for a substring in a PHP string has no result: there is no one searched for, and the result will be a boolean. However, if the desired substring starts from the zero position, this should be emphasized especially in the operations of comparison and analysis of the result. When the result is 0, it is not much further from the result false.

php string search

Unlike JavaScript, with respect to strings, PHP is more conservative, and for it to this day, strings are strings. Working with them is a string function: from simple strpos to complex ones that use regular expressions.

You need to search to:

  • decide on the direction of the algorithm;
  • replace the search with another;
  • check the event;
  • increase the statistics counter, etc.

This is the usual logic of the algorithm; JavaScript went further and recognized the "lines" as objects. But recognition in practice is not enough. If we ignore the concept of "string" and understand the real object as it is, the question "search for a string in a string" by PHP will help us formulate in the context of a real method, for example, an apple can be ripe, not only when it is red.

The classic search for the word β€œred” in the description of the apple is not a guarantee of a reliable answer about the ripeness of the fruit. If the apple is represented not by a string, but by an object, then PHP will search for a string in a string not as strpos, but as a method of a real object. However, there is no guarantee that the strpos function will be used in the method body.

Standard features in custom solutions

A characteristic feature of the usual processing of information (by a person): the position where what is located matters when it is the goal or decision. In all other cases, it does not matter where what is present, it is important that it is indicated and is the basis for further action.

search for substring in php string

PHP indirectly searches for strings in strings in the trim, str_replace, and other functions. An interesting search option is available when using explode / implode together and manipulating arrays.

If you use all the functionality of PHP, the search in the string easily acquires semantics, goes beyond the usual syntax and becomes the reason for gaining real meaning. Manipulating meaning is more familiar to a person, safer for an algorithm, and the transformation of strings into objects is like a transition from machine codes to the world of modern programming languages.

Source: https://habr.com/ru/post/K1297/


All Articles