Questions about string truncation in php or regular expressions

Source: Internet
Author: User
For more information, see php or regular string truncation! The content I read from the database is "XX province, XX city", for example: Jinan city, Shandong province. I want to extract "Jinan". php functions or regular expressions can be used. Ask for advice. It is best to judge based on the word "city". there are not many municipalities directly under the central government, just like the autonomous region. I don't think it is convenient to cut the content here. Help !!! Wait... ------ solution -------------------- PHPcod help: Questions about php or regular expression string truncation
Thank you!
The content I read from the database is "XX province, XX city", for example: Jinan city, Shandong province. I want to extract "Jinan,
Php functions or regular expressions. Ask for advice. It is best to judge based on the word "city", not many municipalities directly under the central government, with the autonomous region,
I don't want to take it for convenience. Help !!! Wait ....

------ Solution --------------------
PHP code
$ Str = 'jinan city, Shandong province '; preg_match ('/. + save (. +) city/', $ str, $ matches); exit ($ matches [1]); // output: Jinan
------ Solution --------------------
$ Ar = array ('XX province XX city ', 'Beijing', 'XX Autonomous Region xx city ');

Foreach ($ ar as $ v ){
Preg_match ('/(? :. * Province |. * District )? (. +) City/', $ v, $ r );
Echo "$ v: $ r [1]
";
}


XX city, xx province: xx
Beijing: Beijing
XX Autonomous Region xx city: xx

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.