Replace and Preg_match when using one or the other?

Asked

Viewed 332 times

1

Doubts about the functions substr e preg_match/preg_match_all.

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

  • It would be possible for me to extract the same with regular expressions only
    are most 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

1 answer

2


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

For printing desired by you the code should be 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 function strpos requires 2 parameters. strpos($procurarAqui, $procurarOque)

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

The substar function() serves to obtain a substring. This function has 3 parameters being the third option.

Parameter 1: The complete string from which the substring is located

Parameter 2: Integer indicating the position where 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 it is possible to 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 know if a given string exists in a text. Let’s assume that you will pick up some text from any source and want to know if there are any emails in this 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, $Ubject 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 is:

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 their registration.

Once understood how to use the function preg_match it is easy to understand the function preg_match_all, because it has the same behavior, with small differences, as the flags used and its search mode, which is done in a global way in the text. The function preg_match was developed to find the required pattern, will be returned and will not be searched in the rest of the text.

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

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

$result = preg_match_all($Pattern, $Subject, $Matches);

  • $Pattern shall be the expression regular
  • $Subject will be the text the expression will search for
  • $Matches will be an array with occurrences

    example - ideone

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.