PHP Learning Note: Enter a sentence to achieve the reverse output of the word

Source: Internet
Author: User
Tags array to string explode

Conventions: Sentences are separated by a space, with a period as the end symbol.

Implementation ideas:

The string is split with the function explode (separator,string,limit), and the last member of the resulting data is split to cut off the symbol. A new array is received in reverse to the string and the period is replaced.

Code implementation:

<?php
/**
* Created by Phpstorm.
* User:administrator
* DATE:2016/9/25 0025
* time:22:29
*/
Strings that need to be reversed
$str = "I am a handsome boy";
$str _len=strlen ($STR);
Echo $str _len; the output is 20, confirm no problem.
Intercept. Character before substr (String,start,length)
$str =substr ($str, 0, $str _len-1);
echo $str; I am a handsome boy, the statement is no problem
Convert to Array
$str _arr=explode (' ', $str);
Var_dump ($str _arr);
Reverse reception
$str _str_new=array_reverse ($str _arr);
Array to String
$str _str_new_str=implode (', $str _str_new);
Echo ' before conversion: '. $str. ' <br/> '. ' After conversion: '. $str _str_new_str. ';

Effect:

PHP Learning Note: Enter a sentence to achieve the reverse output of the word

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.