SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace confusion

    Hi All!

    I created this short listing for replacing ALL the 'non-standard' (Slavic) letters with the extended ASCII codes :
    PHP Code:
    $patterns = array();
    $patterns[0] = '/Č/';
    $replacements[0] = 'Č';
    $patterns[1] = '/Ć/';
    $replacements[1] = 'Ć';
    $patterns[2] = '//';
    $replacements[2] = '';
    $patterns[3] = '/Đ/';
    $replacements[3] = '';
    $patterns[4] = '//';
    $replacements[4] = '';
    $patterns[5] = '//';
    $replacements[5] = '';
    $patterns[6] = '/đ/';
    $replacements[6] = 'đ';
    $patterns[7] = '//';
    $replacements[7] = '';
    $patterns[8] = '/ć/';
    $replacements[8] = 'ć';
    $patterns[9] = '/č/';
    $replacements[9] = 'č';

    $h_transport "TRANSPORTERI";
    $h_couplings "OSOVINSKE SPOJNICE";
    $h_spantech  "SPAN-TECH TRANSPORTERI - NOVO";

    $myFile "content/products_spantech_".$language.".txt";
    $fh fopen($myFile'r');
    $theData fread($fhfilesize($myFile));
    fclose($fh);
    $theData preg_replace($patterns$replacements$theData);

    $h_transport preg_replace($patterns$replacements$h_transport);
    $h_couplings preg_replace($patterns$replacements$h_couplings);
    $h_spantech  preg_replace($patterns$replacements$h_spantech);

    echo 
    $theData;
    echo 
    $h_transport;
    echo 
    $h_couplings;
    echo 
    $h_spantech
    The script replaces even if meets a 'simple' letter c or C to č or Č, when I comment those lines, it goes to the next (ć or Ć) and so on.....
    Why doesn't replace just the given characters like it suppose to do? Am I doing something wrong?

    Thank you for your help!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not quite sure about such special characters but just to replace such individual characters will be replaced with str_replace() only. Consider the following example:
    PHP Code:
    $string "cWMKonDl";
    echo 
    str_replace(array('W''D'), array('www''ddd'), $string); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you rajug, unfortunately str_replace() doesn't work at all in my case....

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    France
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you are trying to do here, it seems the replacement letters are exactly the same as those they are supposed to replace ? Or are you trying to convert unicode symbols to some variant of 8 bit extended ASCII ?

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the problem is if I just type the letters (those with the Slavic 'marks') in my php editor, the browser won't display them correctly. Now I have a plenty of text to insert (copy/paste) and I don't want to replace ALL the characters with the corresponding ASCII codes....
    Hope I was clear enough!

    Full time ADMIN - art community
    Part time coder - dsign

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use UTF-8 and send the page as UTF-8...
    header( 'Content-Type: text/html;charset=utf-8' );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have checked, here's the line from my header :
    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    and the issue is still unsolved...
    Maybe I should find/replace ALL those characters in my plain txt files???

    Full time ADMIN - art community
    Part time coder - dsign

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    ...That is an HTML meta element. It is not the same thing as what I posted.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried with the php line you provided, the result was the same...

    Full time ADMIN - art community
    Part time coder - dsign

  10. #10
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'm getting closer......
    I save the lang_1.php, with some of the patterns like :
    PHP Code:
    $patterns[0] = '/Č/';
    $replacements[0] = 'Č';
    $patterns[1] = '/Ć/';
    $replacements[1] = 'Ć';
    $patterns[8] = '/č/';
    $replacements[8] = 'ć';
    $patterns[9] = '/ć/';
    $replacements[9] = 'č'
    but then when I close my php editor (Macromedia Dreamweaver) and re-open the file, those lines are shown as :
    PHP Code:
    $patterns[0] = '/C/';
    $replacements[0] = 'Č';
    $patterns[1] = '/C/';
    $replacements[1] = 'Ć';
    $patterns[8] = '/c/';
    $replacements[8] = 'ć';
    $patterns[9] = '/c/';
    $replacements[9] = 'č'
    .....
    Why is that the editor allow to type and save or , but doesn't allow ć or č???

    Full time ADMIN - art community
    Part time coder - dsign


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •