Substr and Preg_match when using one or the other?

1

Questions about functions substr e preg_match/preg_match_all.

  • When will I use one or the other?
  • Both remove parts of texts, only% with% remove through regular expressions correct?
  • Could you give me examples of applications of these functions and impressions of the results to my best understanding?

  • I would like to extract the same with regular expressions, are best recommended when they are large string contents,
    correct?

My code:

  $arbitro= "Dentista   Marcovs Salgado de Rorgernaldo Henrique - MG";
    $posicao=strpos("-");
    $posicao=$posicao-2;
    $nomeDentista=substr($arbitro,0,$posicao);
    echo $nomeDentista;
    //impressão desejada por mim
    Dentista Marcovs Salgado de Rorgernaldo Henrique
    
asked by anonymous 29.05.2017 / 19:15

1 answer

2

Strpos () is 3-16 times faster than preg_match () Source

For the impression you want, the code should look like this example - ideone :

$arbitro= "Dentista   Marcovs Salgado de Rorgernaldo Henrique - MG";
$posicao=strpos($arbitro,"-");
$posicao=$posicao-1;
$nomeDentista=substr($arbitro,0,$posicao);
echo $nomeDentista;
  

Note that the strpos function requires 2 parameters. strpos($procurarAqui, $procurarOque)

     

In your case strpos($arbitro,"-")

The substr () function serves to get a substring. This function has 3 parameters, the third being optional.

Parameter 1: The complete string where the substring is

Parameter 2: Integer that indicates the position where the substring starts

Parameter 3 (optional): The size (number of characters) of the substring

By default, the substr () function gets the substring from the given starting point to the end of the string. But we can further delimit the substring if we use the 3rd parameter to indicate how many characters we want to get from the starting point. See the example below:

  $string = "stackoverflow.com";
  $substr1 = substr($string, 5);      // overflow.com
  $substr2 = substr($string, 5, 9);  // over

You can use the preg_match function to find out if a particular string exists in a text. Let's assume you will get some text from any source and wonder if there is any email in that text. See the code:

$subject = "MSeu email é [email protected]";
$pattern = '/([-.a-zA-Z]{1,30})@([-.a-zA-Z]{1,30})([.]{1})([-.a-zA-Z]{1,10})/';
$result = preg_match($pattern, $subject);

In the above code, $ subject is the text where you will search, $ pattern is a regular expression (ER) created to match an email. The return of the preg_match function to the $ result variable are:

0 – caso a ER não case com nenhuma parte do texto
1 – caso a ER case com alguma parte do texto

With this example, you can check if a user has entered a valid email in your account.

Once you understand how to use the preg_match function, it is easy to understand the preg_match_all function, since it has the same behavior, with small differences, such as the flags used and its search mode, which is made from a way in the text. The preg_match function was developed as soon as it finds the required pattern, it will be returned and no search will be made for the rest of the text.

The function preg_match_all () will return an integer with the number of occurrences found by the regular expression.

The function accepts 5 parameters, 3 of which are mandatory and 2 are optional.

$ result = preg_match_all ($ pattern, $ subject, $ matches);

  • $ pattern will be the expression regultar
  • $ subject will be the text that the expression will search
  • $ matches will be an array with occurrences

    example - ideone

  

If you do not need to use ER and just want to check if a string is inside another you should use other functions like strpos or strstr, since they are faster.

    
29.05.2017 / 21:54