PHP/String/strstr

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

string strstr ( string haystack, string needle ) finds the first occurrence of a substring inside another string (parameter one)

   <source lang="html4strict">

<?

   $string = "http://www.example.ru/mypage.php";
   $newstring = strstr($string, "www");

?>

 </source>
   
  


strstr function demo

   <source lang="html4strict">

<?php

   $haystack = "is a test";
   $pos = strstr ($haystack, "is");
   if (!$pos)
     echo "String not found\n";
   else
     echo "String found: $pos\n";

?>

 </source>
   
  


strstr() function returns the remainder of string beginning at the first occurrence.

   <source lang="html4strict">

Its syntax is: string strstr (string string, string occurrence) <? $url = "http://www.wbex.ru"; $domain = strstr($url, "."); print $domain; ?>

 </source>
   
  


strstr.php

   <source lang="html4strict">

<?php

  $url = "sales@example.ru"; 
  echo ltrim(strstr($url, "@"),"@");

?>

 </source>
   
  


strtr() function converts all characters contained in destination to their corresponding character matches in source.

   <source lang="html4strict">

Its syntax: string strtr (string string, string source, string destination) <?

$source = array("<title>" => "

", "</title>" => "

"); $string = "

Today In PHP-Powered News

";
   print strtr($string, $source);

?>

 </source>
   
  


strtr.php

   <source lang="html4strict">

<?php

  $table = array("" => "", "</b>" => "");
  $html = "<b>Today";
  echo strtr($html, $table);

?>

 </source>