PHP/String/Sub string

Материал из Web эксперт
Перейти к: навигация, поиск

Substring count

<?php
   $buzzwords = array("ex", "te", "xt");
$talk = <<< talk
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text  
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text 
talk;
   foreach($buzzwords as $bw) {
      echo "The word $bw appears ".substr_count($talk,$bw)." time(s).<br />";
   }
?>



Substring replace

<?php
   $ccnumber = "1234567899991111";
   echo substr_replace($ccnumber,"************",0,12);
?>



substr($string, 3)

<?
$alphabet_test = "abcdefghijklmnop";
print("3: " . substr($alphabet_test, 3));
?>



substr($String, 3, 5)

<?
$alphabet_test = "abcdefghijklmnop";
print("3, 5: " . substr($alphabet_test, 3, 5));
?>



substr($String, (2)

<?
$alphabet_test = "abcdefghijklmnop";
print("-3, 5: " . substr($alphabet_test, -3, 5));
?>



substr($String, -3, -5) (3)

<?
$alphabet_test = "abcdefghijklmnop";
print("-3, -5: " . substr($alphabet_test, -3, -5));
?>



substr($String, 3, -5) (4)

<?
$alphabet_test = "abcdefghijklmnop";
print("3, -5: " . substr($alphabet_test, 3, -5));
?>