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