Apa gunanya fungsi em strpos )

Strops() adalah fungsi built-in dari PHP. Ini digunakan untuk menemukan posisi kejadian pertama dari sebuah string di dalam string atau substring lain dalam sebuah string

Sintaksis

ParameterDescriptionRequired/OptionalstringTentukan string yang akan dicari. Startfind Tentukan string yang akan ditemukan. Requiredstart Tentukan di mana untuk memulai pencarian. Opsional

Fungsi ini akan membantu kita menemukan posisi numerik kemunculan pertama jarum di tumpukan jerami

Saat menulis skrip PHP, Anda sering perlu mencari string untuk potongan teks tertentu. Misalnya, Anda mungkin menulis mesin telusur untuk menelusuri halaman konten, atau Anda mungkin ingin mengetahui apakah URL atau alamat email berisi nama domain tertentu

PHP memberi Anda banyak fungsi berguna untuk mencari string. Pada artikel ini Anda melihat

  • 
    $myString = 'Hello, there!';
    
    if ( strstr( $myString, 'Goodbye' ) ) {
      echo "Text found";
    } else {
      echo "Text not found";
    }
    
    1 untuk mengetahui apakah beberapa teks ada dalam string
  • 
    $myString = 'Hello, there!';
    
    if ( strstr( $myString, 'Goodbye' ) ) {
      echo "Text found";
    } else {
      echo "Text not found";
    }
    
    2 dan
    
    $myString = 'Hello, there!';
    
    if ( strstr( $myString, 'Goodbye' ) ) {
      echo "Text found";
    } else {
      echo "Text not found";
    }
    
    3 untuk menemukan posisi beberapa teks dalam sebuah string
  • 
    $myString = 'Hello, there!';
    
    if ( strstr( $myString, 'Goodbye' ) ) {
      echo "Text found";
    } else {
      echo "Text not found";
    }
    
    4 untuk mengetahui berapa kali beberapa teks muncul dalam sebuah string

Pencarian teks sederhana dengan $myString = 'Hello, there!'; if ( strstr( $myString, 'Goodbye' ) ) { echo "Text found"; } else { echo "Text not found"; } 1

Fungsi


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1 PHP hanya membutuhkan string untuk dicari, dan potongan teks untuk dicari. Jika teks ditemukan, ia mengembalikan bagian string dari karakter pertama yang cocok hingga akhir string


$myString = 'Hello, there!';
echo strstr( $myString, 'llo' );  // Displays "llo, there!"
_

Jika teks tidak ditemukan maka


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1 mengembalikan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
8. Anda dapat menggunakan fakta ini untuk menentukan apakah potongan teks ada di dalam string atau tidak


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}

Kode di atas ditampilkan

Text not found

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1 peka huruf besar/kecil — misalnya,
Text not found
0 tidak akan cocok dengan
Text not found
1. Jika Anda tidak peduli tentang pencocokan huruf besar-kecil, gunakan versi yang tidak peka huruf besar-kecil,
Text not found
2, sebagai gantinya

Mencari posisi pertandingan. $myString = 'Hello, there!'; if ( strstr( $myString, 'Goodbye' ) ) { echo "Text found"; } else { echo "Text not found"; } 2 dan $myString = 'Hello, there!'; if ( strstr( $myString, 'Goodbye' ) ) { echo "Text found"; } else { echo "Text not found"; } 3


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 mengambil 2 argumen yang sama dengan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1. Namun, alih-alih mengembalikan sebagian dari string, ia mengembalikan indeks karakter pertama dari teks yang cocok dalam string


$myString = 'Hello, there!';
echo strpos( $myString, 'llo' );  // Displays "2"

Dalam kode di atas,


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 menemukan teks
Text not found
8 dalam string target dimulai dari karakter ke-3, sehingga mengembalikan
Text not found
9. (Ingat bahwa posisi indeks karakter dalam string dimulai dari 0, bukan 1. )

Berhati-hatilah saat menggunakan


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
_2 untuk memeriksa keberadaan kecocokan. Kode berikut salah menampilkan “Tidak ditemukan”, karena

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 mengembalikan

$myString = 'Hello, there!';
echo strpos( $myString, 'llo' );  // Displays "2"
2, yang setara dengan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
8 di PHP


$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) ) {
  echo "Found";
} else {
  echo "Not found";
}

Untuk memperbaikinya, pastikan Anda menguji secara eksplisit untuk


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
8 dengan menggunakan operator

$myString = 'Hello, there!';
echo strpos( $myString, 'llo' );  // Displays "2"
5 atau

$myString = 'Hello, there!';
echo strpos( $myString, 'llo' );  // Displays "2"
6. Kode berikut dengan benar menampilkan "Ditemukan"


$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) !== false ) {
  echo "Found";
} else {
  echo "Not found";
}

Anda dapat meneruskan argumen ketiga ke


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 untuk menentukan posisi indeks untuk memulai pencarian


$myString = 'Hello, there!';
echo strpos( $myString, 'e' ) . '<br />';     // Displays "1"
echo strpos( $myString, 'e', 2 ) . '<br />';  // Displays "9"

Fungsi


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
3 sangat mirip dengan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2. Satu-satunya perbedaan adalah menemukan kecocokan terakhir dalam string, bukan yang pertama


$myString = 'Hello, there!';
echo strpos( $myString, 'e' ) . "<br />";   // Displays "1"
echo strrpos( $myString, 'e' ) . "<br />";  // Displays "11"
Seperti

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1, fungsi

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 dan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
3 peka huruf besar/kecil. Padanannya yang tidak peka huruf besar kecil adalah

$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) ) {
  echo "Found";
} else {
  echo "Not found";
}
3 dan

$myString = 'Hello, there!';
if ( strpos( $myString, 'Hello' ) ) {
  echo "Found";
} else {
  echo "Not found";
}
4 masing-masing

Menghitung kecocokan dengan $myString = 'Hello, there!'; if ( strstr( $myString, 'Goodbye' ) ) { echo "Text found"; } else { echo "Text not found"; } 4

Anda dapat menggunakan fungsi


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
_4 PHP untuk menemukan berapa kali potongan teks muncul di string target


$myString = 'Short stories';
echo substr_count( $myString, 'or' );  // Displays "2"

Seperti


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
2 dan

$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
3, Anda dapat memberikan argumen ketiga opsional. posisi indeks untuk memulai pencarian. Sebagai contoh


$myString = 'Short stories';
echo substr_count( $myString, 'or', 6 );  // Displays "1"

Anda juga bisa memberikan argumen keempat opsional. jumlah karakter setelah posisi ofset untuk mencari teks. Sebagai contoh


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
0

Pada artikel ini Anda telah melihat cara mencari string di PHP. Anda telah menjelajahi fungsi


$myString = 'Hello, there!';

if ( strstr( $myString, 'Goodbye' ) ) {
  echo "Text found";
} else {
  echo "Text not found";
}
1 untuk mencari tahu apakah potongan teks ada dalam sebuah string; . Selamat mengkode

Apa gunanya fungsi strpos () di PHP?

Fungsi strpos() menemukan posisi kemunculan pertama string di dalam string lain . Catatan. Fungsi strpos() bersifat case-sensitive.

Apa kegunaan fungsi strlen() dan strpos() pada PHP?

Fungsi String PHP

Apa strstr () di PHP?

Fungsi strstr() mencari kemunculan pertama string di dalam string lain . Catatan. Fungsi ini aman untuk biner. Catatan. Fungsi ini peka huruf besar-kecil. Untuk pencarian case-insensitive, gunakan fungsi stristr().

Apa perbedaan antara Strpos dan Stripos di PHP?

Definisi dan Penggunaan . strripos() - Menemukan posisi kejadian terakhir dari sebuah string di dalam string lain (case-insensitive) strpos() - Menemukan posisi kejadian pertama dari sebuah string di dalam string lain (case-sensitive)