Jump to content
Sign in to follow this  
cmd

PHP: города в предложном падеже

Recommended Posts

Просто оставлю тут:

 

Киев -> в Киеве

Пермь -> в Перми

Москва -> в Москве

Питер -> в Питере

function to_prepositional($str) {	
	
	$replace = array();
	$replace['2'][] = array('ия','ии');
	$replace['2'][] = array('ия','ии');
	$replace['2'][] = array('ий','ом');
	$replace['2'][] = array('ое','ом');
	$replace['2'][] = array('ая','ой');
	$replace['2'][] = array('ль','ле');
	$replace['1'][] = array('а','е');
	$replace['1'][] = array('о','е');
	$replace['1'][] = array('и','ах');
	$replace['1'][] = array('ы','ах');
	$replace['1'][] = array('ь','и');
	
	foreach ($replace as $length => $replacement) {
		$str_length = mb_strlen($str, 'UTF-8');
		$find = mb_substr($str, $str_length - $length, $str_length, 'UTF-8');
		foreach($replacement as $try) {
			if ( $find == $try[0] ) {
				$str = mb_substr($str, 0, $str_length - $length, 'UTF-8');
				$str .= $try['1'];
				return $str;
			}
		}
	}
	if ($find == 'е') {
		return $str;
	} else {
		return $str.'е';
	}
	
}

Share this post


Link to post
Share on other sites

Осло -> в Осле ? :-)

города (деревни и село) оканчивающиеся на -и, -о, -е(ё), -ово, -ево, -ино, -ыно, не склоняются

Share this post


Link to post
Share on other sites

судя по этому 

$replace['1'][] = array('о','е');

склоняются:-)

Share this post


Link to post
Share on other sites

 

Просто оставлю тут:

Просто отвечу тут :)

 

Если очень это нужно, то есть

http://sourceforge.net/projects/phpmorphy

 

Посмотри поиск с морфологией

Share this post


Link to post
Share on other sites

Вам шашечки или ехать? :) Мне нужно было 30 крупных городов Украины и РФ. Ростов-на-Дону заменил на Ростов и доволен. А если кому надо Улан-Удэ и Осло, то тут, конечно, парой стоковых функций не отделаешься. True story.

Share this post


Link to post
Share on other sites

Глупости это все. Только массив "вручную" переведенных, все остальное - от лукавого.

chukcha, А вот это круто! Спасибо!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.