php - Understanding strpos, preg_replace, and preg_match functions

one text

Solution:

strpos is a function that allows you to find if a specific word or combination of characters present in the string you provided. It's like yes/no and additionally, it can give you it's start location. It is important to note that it will return false when nothing is found or a number, including 0 when the match is found.

So this function is very good for simple scenarios like does it have letters app in the word apple? the answer is yes, and the start position is 0.

In the example, you provided if (strpos($link, 'rel') === false) it searches if word rel is present, and it doesn't matter in what position.

preg_match is used for much complex searching using regex, capable of searching multiple conditions, groups and more. To give an example of what it can search: the word apprehensive does it starts with app and ends with sive? - answer yes. If strpos would've been used, it would say yes to all such cases due to the app being present but the ending is never checked as it's incapable of such thing. preg_match can commonly be used to get what's inside of an attribute, so it grabs rel="[what's here]".

Preg_replace is replacing something using regex for complex searching first, then do the replacement of a match.

I would always advise to read up documentation provided by php for the functions and it's accepted arguments.

Information about strpos

Information about preg_replace

Information about preg_match

Information about regex (takes a good while to learn!)

Source