PHP Intercept String function strtr/str_replace

Source: Internet
Author: User
/**

* 1. Strtr converting a specified character

*

* String Strtr (String $str, String $from, String $to)

* String Strtr (string $str, array $replace _pairs)

*

* This function returns a copy of Str and converts the character specified in the From to the corresponding character in to.

* If the from and to lengths are not equal, the extra character parts will be ignored.

*/


$str = ' http://flyer0126.iteye.com/';


Echo strtr ($str, ' IT ', ' Java ');

OUTPUT:HTTP://FLYER0126.ITEYE.COM/STRTR Case Sensitive


If the from and to lengths are not equal, the extra character parts are ignored

Echo strtr ($str, ' It ', ' Java ');

output:haap://flyer0126.jaeye.com/

Iteye---Jaeye It's just for JA.

HTTP---HAAP word-by-character replacement of the corresponding position, which does not fit our purpose


Echo strtr ($str, ' it ', ');

output:http://flyer0126.iteye.com/No replacement


Echo strtr ($str, ' it ', ');

output:http://flyer0126. teye.com/can be replaced


/**

* The From->to way of function STRTR is summarized as follows:

* 1. Case sensitive;

* 2. When the form and to length are unequal, the extra characters will be ignored, can not be changed less, and can not be changed more;

* 3. Replace the corresponding position by character;

* 4. cannot be replaced with empty and can be replaced by spaces.

*/


In comparison, the latter approach is more appropriate

$replace _pairs = Array (

'/http '/' = ',

' It ' = ' java '

);

Echo Strtr ($str, $replace _pairs);

output:flyer0126.javaeye.com/replacement success, in line with the original replacement


/**

* 2. function Str_replace

* Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])

*/


Echo str_replace (' It ', ' Java ', $str);

output:http://flyer0126.javaeye.com/

echo str_replace (Array (' http ', ': ', '//', '/'), ', $str);

Output:flyer0126.iteye.com

echo str_replace (Array (' HTTP ', ' it ', '/'), Array (' HTTPS ', ' Java ', '), $STR);

Output:https:flyer0126.javaeye.com

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.