PHP/String/strstr

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

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

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



strstr function demo

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



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

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



strstr.php

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



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

 
Its syntax: string strtr (string string, string source, string destination)
<?
    $source = array("<title>" => "<h1>", "</title>" => "</h1>");
    $string = "<h1>Today In PHP-Powered News</h1>";
    print strtr($string, $source);
?>



strtr.php

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