تابع اصلاح رشته فارسی




این تابع یک رشته یونی‌کد را گرفته و نویسه‌های غیرفارسی موجود در آن را با نویسه‌های فارسی عوض می‌کند.

از این تابع می‌توانید برای اصلاح رشته‌های ورودی کاربران استفاده کنید.



JavaScript

function fixPersianString(text) {

    if (text == null)
        return null;

    text = text.replace(/\u0660/g, '\u06F0'); // ۰
    text = text.replace(/\u0661/g, '\u06F1'); // ۱
    text = text.replace(/\u0662/g, '\u06F2'); // ۲
    text = text.replace(/\u0663/g, '\u06F3'); // ۳
    text = text.replace(/\u0664/g, '\u06F4'); // ۴
    text = text.replace(/\u0665/g, '\u06F5'); // ۵
    text = text.replace(/\u0666/g, '\u06F6'); // ۶
    text = text.replace(/\u0667/g, '\u06F7'); // ۷
    text = text.replace(/\u0668/g, '\u06F8'); // ۸
    text = text.replace(/\u0669/g, '\u06F9'); // ۹
    text = text.replace(/\u0643/g, '\u06A9'); // ک
    text = text.replace(/\u0649/g, '\u06CC'); // ی
    text = text.replace(/\u064A/g, '\u06CC'); // ی
    text = text.replace(/\u06C0/g, '\u0647\u0654'); // هٔ

    return text;
}


C#

public static string FixPersianString(string text) {

    if (text == null)
        return null;

    text = text.Replace("\u0660", "\u06F0"); // ۰
    text = text.Replace("\u0661", "\u06F1"); // ۱
    text = text.Replace("\u0662", "\u06F2"); // ۲
    text = text.Replace("\u0663", "\u06F3"); // ۳
    text = text.Replace("\u0664", "\u06F4"); // ۴
    text = text.Replace("\u0665", "\u06F5"); // ۵
    text = text.Replace("\u0666", "\u06F6"); // ۶
    text = text.Replace("\u0667", "\u06F7"); // ۷
    text = text.Replace("\u0668", "\u06F8"); // ۸
    text = text.Replace("\u0669", "\u06F9"); // ۹
    text = text.Replace("\u0643", "\u06A9"); // ک
    text = text.Replace("\u0649", "\u06CC"); // ی
    text = text.Replace("\u064A", "\u06CC"); // ی
    text = text.Replace("\u06C0", "\u0647\u0654"); // هٔ

    return text;
}


VB.NET

Public Shared Function FixPersianString(ByVal text As String) As String
    
    If text Is Nothing Then
        Return Nothing
    End If
    
    text = text.Replace(ChrW(&H660), ChrW(&H6F0)) ' ۰
    text = text.Replace(ChrW(&H661), ChrW(&H6F1)) ' ۱
    text = text.Replace(ChrW(&H662), ChrW(&H6F2)) ' ۲
    text = text.Replace(ChrW(&H663), ChrW(&H6F3)) ' ۳
    text = text.Replace(ChrW(&H664), ChrW(&H6F4)) ' ۴
    text = text.Replace(ChrW(&H665), ChrW(&H6F5)) ' ۵
    text = text.Replace(ChrW(&H666), ChrW(&H6F6)) ' ۶
    text = text.Replace(ChrW(&H667), ChrW(&H6F7)) ' ۷
    text = text.Replace(ChrW(&H668), ChrW(&H6F8)) ' ۸
    text = text.Replace(ChrW(&H669), ChrW(&H6F9)) ' ۹
    text = text.Replace(ChrW(&H643), ChrW(&H6A9)) ' ک
    text = text.Replace(ChrW(&H649), ChrW(&H6CC)) ' ی
    text = text.Replace(ChrW(&H64A), ChrW(&H6CC)) ' ی
    text = text.Replace(ChrW(&H6C0), ChrW(&H647) + ChrW(&H654)) ' هٔ

    Return text

End Function


TSQL

CREATE FUNCTION dbo.FixPersianString(@Text NVARCHAR(255)) RETURNS NVARCHAR(255)
AS
BEGIN
    
    SELECT @Text = REPLACE(@Text, NCHAR(0x660), NCHAR(0x6F0)) -- ۰
    SELECT @Text = REPLACE(@Text, NCHAR(0x661), NCHAR(0x6F1)) -- ۱
    SELECT @Text = REPLACE(@Text, NCHAR(0x662), NCHAR(0x6F2)) -- ۲
    SELECT @Text = REPLACE(@Text, NCHAR(0x663), NCHAR(0x6F3)) -- ۳
    SELECT @Text = REPLACE(@Text, NCHAR(0x664), NCHAR(0x6F4)) -- ۴
    SELECT @Text = REPLACE(@Text, NCHAR(0x665), NCHAR(0x6F5)) -- ۵
    SELECT @Text = REPLACE(@Text, NCHAR(0x666), NCHAR(0x6F6)) -- ۶
    SELECT @Text = REPLACE(@Text, NCHAR(0x667), NCHAR(0x6F7)) -- ۷
    SELECT @Text = REPLACE(@Text, NCHAR(0x668), NCHAR(0x6F8)) -- ۸
    SELECT @Text = REPLACE(@Text, NCHAR(0x669), NCHAR(0x6F9)) -- ۹
    SELECT @Text = REPLACE(@Text, NCHAR(0x643), NCHAR(0x6A9)) -- ک
    SELECT @Text = REPLACE(@Text, NCHAR(0x649), NCHAR(0x6CC)) -- ی
    SELECT @Text = REPLACE(@Text, NCHAR(0x64A), NCHAR(0x6CC)) -- ی
    SELECT @Text = REPLACE(@Text, NCHAR(0x6C0), NCHAR(0x647) + NCHAR(0x654)) -- هٔ
    
    RETURN @Text
END


Python

تست کد def fixPersianString(text):
      if text is None:
         return None

      text = text.replace('\xd9\xa0', '\xdb\xb0') #۰
      text = text.replace('\xd9\xa1', '\xdb\xb1') #۱
      text = text.replace('\xd9\xa2', '\xdb\xb2') #۲
      text = text.replace('\xd9\xa3', '\xdb\xb3') #۳
      text = text.replace('\xd9\xa4', '\xdb\xb4') #۴
      text = text.replace('\xd9\xa5', '\xdb\xb5') #۵
      text = text.replace('\xd9\xa6', '\xdb\xb6') #۶
      text = text.replace('\xd9\xa7', '\xdb\xb7') #۷
      text = text.replace('\xd9\xa8', '\xdb\xb8') #۸
      text = text.replace('\xd9\xa9', '\xdb\xb9') #۹
      text = text.replace('\xd9\x83', '\xda\xa9') #ک
      text = text.replace('\xd9\x89', '\xdb\x8c') #ی
      text = text.replace('\xd9\x8a', '\xdb\x8c') #ی
      text = text.replace('\xdb\x80', '\xd9\x87\xd9\x94') #هٔ
      #using the translate method & a translation table could be an alternative way.
   
      return text


Ruby

تست کد def fixPersianString(text)
      if text == nil:
            return nil
      end

      text.gsub!("\xd9\xa0", "\xdb\xb0") #۰
      text.gsub!("\xd9\xa1", "\xdb\xb1") #۱
      text.gsub!("\xd9\xa2", "\xdb\xb2") #۲
      text.gsub!("\xd9\xa3", "\xdb\xb3") #۳
      text.gsub!("\xd9\xa4", "\xdb\xb4") #۴
      text.gsub!("\xd9\xa5", "\xdb\xb5") #۵
      text.gsub!("\xd9\xa6", "\xdb\xb6") #۶
      text.gsub!("\xd9\xa7", "\xdb\xb7") #۷
      text.gsub!("\xd9\xa8", "\xdb\xb8") #۸
      text.gsub!("\xd9\xa9", "\xdb\xb9") #۹
      text.gsub!("\xd9\x83", "\xda\xa9") #ک
      text.gsub!("\xd9\x89", "\xdb\x8c") #ی
      text.gsub!("\xd9\x8a", "\xdb\x8c") #ی
      text.gsub!("\xdb\x80", "\xd9\x87\xd9\x94") #هٔ
      #use the gsub() method instead of gsub!() if you need a fixed string copy.

     return text
end


PHP

تست کد function fixPersianString($text){
       if(is_null($text))
          return null;
       $replacePairs = array(
                chr(0xD9).chr(0xA0) => chr(0xDB).chr(0xB0),
                chr(0xD9).chr(0xA1) => chr(0xDB).chr(0xB1),
                chr(0xD9).chr(0xA2) => chr(0xDB).chr(0xB2),
                chr(0xD9).chr(0xA3) => chr(0xDB).chr(0xB3),
                chr(0xD9).chr(0xA4) => chr(0xDB).chr(0xB4),
                chr(0xD9).chr(0xA5) => chr(0xDB).chr(0xB5),
                chr(0xD9).chr(0xA6) => chr(0xDB).chr(0xB6),
                chr(0xD9).chr(0xA7) => chr(0xDB).chr(0xB7),
                chr(0xD9).chr(0xA8) => chr(0xDB).chr(0xB8),
                chr(0xD9).chr(0xA9) => chr(0xDB).chr(0xB9),
                chr(0xD9).chr(0x83) => chr(0xDA).chr(0xA9),
                chr(0xD9).chr(0x89) => chr(0xDB).chr(0x8C),
                chr(0xD9).chr(0x8A) => chr(0xDB).chr(0x8C),
                chr(0xDB).chr(0x80) => chr(0xD9).chr(0x87) . chr(0xD9).chr(0x94));
       return strtr($text, $replacePairs);
   }


Delphi

function FixPersianString(Text :WideString):WideString;
begin
  result:= text;
  
  if result <> '' then
  begin
    result:= ReplaceStr(result, widechar($660), widechar($6F0));
    result:= ReplaceStr(result, widechar($661), widechar($6F1));
    result:= ReplaceStr(result, widechar($662), widechar($6F2));
    result:= ReplaceStr(result, widechar($663), widechar($6F3));
    result:= ReplaceStr(result, widechar($664), widechar($6F4));
    result:= ReplaceStr(result, widechar($665), widechar($6F5));
    result:= ReplaceStr(result, widechar($666), widechar($6F6));
    result:= ReplaceStr(result, widechar($667), widechar($6F7));
    result:= ReplaceStr(result, widechar($668), widechar($6F8));
    result:= ReplaceStr(result, widechar($669), widechar($6F9));
    result:= ReplaceStr(result, widechar($643), widechar($6A9));
    result:= ReplaceStr(result, widechar($649), widechar($6CC));
    result:= ReplaceStr(result, widechar($64A), widechar($6CC));
    result:= ReplaceStr(result, widechar($6C0), widechar($647) + widechar($654));
  end;
  
end;
برای به دست آوردن معادل یونی‌کدی کاراکترها می‌توانید از این ابزار استفاده کنید.

مطالب مرتبط






Feed نظرات

 
+
| mahdi mahdi | 26 اردیبهشت 1388
خیلی مفید و عالی! ممنون!
 
+
| soheilpro soheilpro | 26 اردیبهشت 1388
لطفاً کسانی که با زبان‌های دیگه برنامه‌نویسی آشنایی دارند این ویکی رو کامل‌تر کنند.
 
+
| afsharm afsharm | 28 اردیبهشت 1388
شروع خوبیه!
 
+
| benyblack benyblack | 16 خرداد 1388
مفید بود ممنون
 
+
| roozy_danlod roozy_danlod | 19 خرداد 1388
ببخشید این کد رو باید کجای قالب قرار داد؟ سیستم وردپرس هست
 
+
| lajevardi lajevardi | 12 تیر 1388
در صورتی که از بسته‌ی فارسی‌ساز استفاده کنید، وردپرس قسمت زیادی از کار رو براتون انجام میده.
در ضمن هنوز کسی کد PHP این تابع رو ننوشته!
 
+
| classicboyir classicboyir | 30 مهر 1388
مرسی خیلی عالی و مفید بود !
 
+
| sassan sassan | 25 آبان 1388
دوستان تا آنجا که می‌بینم این function ها شماره‌های انگلیسی را درست نمی‌کنند، بهتر نیست این کار نیز افزوده شود؟
 
+
| lajevardi lajevardi | 10 آذر 1388
به این دلیل که این‌ها توابع "اصلاح رشته فارسی" هستن و قاعدتا کاری به تبدیل "اعداد لاتین" ندارن :)

[ویرایش شده در 14 آذر 1388]
 
+
| sassan sassan | 22 آذر 1388
بله به همین دلیل پیشنهاد دادم، همین جا را ببینید نوشته است 30 مهر 1388 ولی بر پایه آن چیزی که از این function می‌خواهیم، درست آن ۳۰ مهر ۱۳۸۸ است.
 
+
| lajevardi lajevardi | 15 روز قبل
:)


برای ارسال نظر باید ابتدا وارد سایت شوید.