PHP language has been actively used among web developers for many years. As in every software language, PHP has best practices and little-known errors/misuses. In this lesson PHP strpos() We will talk about how to use its function in the best way and little-known information.
Don't Compare with True
in PHP language strpos() function returns the starting position of the searched text. strpos() When we use the function while searching whether the target is found in the text, when the target is found 1, when not found 0 returns the code. If the target was found at the very beginning of the searched text, 0 returns. This is not exactly a bug in PHP, but as a result, when the presence of the target text is searched in the if query written as the example below, the target text is at the beginning. 0 It will cause an error in the software because it will return .
Example:
$data = "Onur Özden Web Çözümleri";
$search = "Onur";
if (strpos($data, $search) === true) {
echo "Metinde '{$search}' ifadesi geçiyor";
} else { // Bu blok çalışacak
echo "Metinde '{$search}' ifadesi geçmiyor";
}
// Çıktı: Metinde 'Onur' ifadesi geçmiyorIn the example, the else block worked. This is wrong! Because the name " Onur" is mentioned in the text we used in the example. This is because strpos() In the function, if the searched text is at the beginning of the data, the function is called "0 is to return the answer. So how do we overcome this problem? Do we need to use a different function? No. There is no need for this. There is a very simple solution to this problem. Let's examine the example and detail it.
$data = "Onur Özden Web Çözümleri";
$search = "Onur";
if (strpos($data, $search) !== false) { // Bu blok çalışacak
echo "Metinde '{$search}' ifadesi geçiyor";
} else {
echo "Metinde '{$search}' ifadesi geçmiyor";
}
// Çıktı: Metinde 'Onur' ifadesi geçiyorIn this example, the comparison in the if block worked correctly. The reason is that all popular programming languages, including PHP, and even software logic 0 value false, 1 value true is considered as. Since the return value is 0, when a boolean type comparison is made in the if block, the value 0 is processed as false and this error occurs.
Use Strict Comparison When Comparing with False
There is another very important detail that we need to pay attention to in the previous example. If we make an equality comparison (loose comparison) instead of a strict comparison, we will encounter the same error again. Because equality comparison does not question the type of data, equivalence comparison also questions the type of data. Let's examine an incorrect example of what we are talking about. Let's do an example without an equivalence comparison and see the result
$data = "Onur Özden Web Çözümleri";
$search = "Onur";
if (strpos($data, $search) != false) {
echo "Metinde '{$search}' ifadesi geçiyor";
} else { // Bu blok çalışacak
echo "Metinde '{$search}' ifadesi geçmiyor";
}
// Çıktı: Metinde 'Onur' ifadesi geçmiyorWe Can Manage Case Sensitivity
The strpos() function is case sensitive by default. If we want to remove this sensitivity, we must use the stripos() function. Let's look at an example of the stripos() function:
$data = "Onur Özden Web Çözümleri";
$search = "onur";
if (stripos($data, $search) !== false) { // Bu blok çalışacak
echo "Metinde '{$search}' ifadesi geçiyor";
} else {
echo "Metinde '{$search}' ifadesi geçmiyor";
}
// Çıktı: Metinde 'Onur' ifadesi geçiyorWe Can Check Whether the Searched Text Occurs More Than Once with the strpos() function
1st example:
We can check how many times the target text appears in the data with the strpos() function. In the 1st example, we will show the creative but simpler and basic way to do this, and in the 2nd example, we will show the best way, which is much more practical.
$data = "Onur Özden Web Çözümleri, Onur Özden tarafından kurulmuştur. Onur Özden ve ekibi ile web yazılım geliştirme, wordpress teknik destek ve seo hizmetleri sunar.";
$search = "Onur";
// undefined hatası almamak için değişkenleri tanımla ve varsayılan olarak 0 değerini ver;
$position = 0;
$count = 0;
while (($position = strpos($data, $search, $position)) !== false) {
$count++; // Kaçıncı kez geçtiğini say
$position++; // Sonraki arama için pozisyonu artır
}
// Sonucu yazdır
echo "Veri içerisinde toplam {$count} adet '{$search}' kelimesi geçiyor<br>";
// Çıktı: Veri içerisinde toplam 3 adet 'Onur' kelimesi geçiyor2nd example:
$data = "Onur Özden Web Çözümleri, Onur Özden tarafından kurulmuştur. Onur Özden ve ekibi ile web yazılım geliştirme, wordpress teknik destek ve seo hizmetleri sunar.";
$search = "Onur";
// substr_count fonksiyonu ile $search ifadesinin $data içindeki toplam sayısını hesapla
$count = substr_count($data, $search);
// veri içerisinde hedef kelime geçmiyorsa "0 adet geçiyor" gibi anlamsız bir ifade çıktılanmaması için koşul
if ($count > 0) { // veride hedef ifade geçtiği için bu blok çalışacak
echo "Veri içerisinde toplam {$count} adet '{$search}' kelimesi geçiyor";
} else {
echo "Veri içerisinde '{$search}' kelimesi geçmiyor.";
}
// Çıktı: Veri içerisinde toplam 3 adet 'Onur' kelimesi geçiyorsubstr_count() In short, the function calculates how many subtexts are passed in the target and how many times. You can find detailed documentation and examples of this function here -> https://www.php.net/manual/function.substr-count.php
About strpos()
In this article strpos() We learned how to use its function in the best way and correct the most common misuse. strpos() with its function stripos() and substr_count() We also got to know their functions. Stay tuned for the rest of our series on best quality coding approaches.



