I cant process the posted string

I have this code that includes a function which is supposed to conver characters like “ğ,ç,ü…” to “g,c,u” . The problem is that when I write an internal string it is converted but cant convert the posted string

<?php
$meslek0=$_POST['meslek']; 



$internal='ğöşc';
echo '<br>internal original: '.$internal;

echo '<br>posted original: '.$meslek0;

$meslek0=replace_tr($meslek0);
$internal=replace_tr($internal);

echo '<br>internal converted:'.$internal;
echo '<br>posted converted:'.$meslek0;



function replace_tr($text) {
$text = trim($text);
$search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
$replace = array('c','c','g','g','i','i','o','o','s','s','u','u','-');
$new_text = str_replace($search,$replace,$text);
return $new_text;
}  



 ?>

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

Hi zmuteber,

Welcome to SitePoint.

Try this:

<form action="?"  method="post">
   <div>
      mesleko: <input type="text" name="mesleko" value="" />
      <br>
      <input type="submit" value="Submit" />
   </div>
</form>      

 <?php 
   echo '<pre>$_POST items = ', print_r($_POST, true), '</pre>';

   $internal='ğöşc';
   $mesleko = isset($_POST['mesleko']) && ! empty($_POST['mesleko']) 
   
   ? $_POST['mesleko'] : '$mesleko NOT SET ???';

   $internalResult = replace_tr($internal);
   $meslekoResult  = replace_tr($mesleko);

   echo '<dl>';
      echo '<dt>internal original: </dt>';
         echo '<dd>' .$internal  .'</dd>';
     echo '<dt>posted original </dt>'; 
        echo '<dd>' .$mesleko .'</dd>';
        echo '<dd><br /></dd>';

     echo '<dt>internal converted: </dt>';
        echo '<dd>' .$internalResult .'</dd>';
     echo '<dt>posted converted:  </dt>';
        echo '<dd>' .$meslekoResult .'</dd>';
   echo '</dl>';

//=============================
function replace_tr($text=NULL)
{
   $result = NULL;

   $text        = trim($text);
   $search   = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
   $replace  = array('c','c','g','g','i','i','o','o','s','s','u','u','-');

   $result = str_replace($search, $replace, $text);

   return $result;
}          

Output:

$_POST items = Array
(
    [mesleko] => ğöşc
)

internal original:
    ğöşc
posted original
    ğöşc

internal converted:
    gosc
posted converted:
    gosc
1 Like

It works well, thank you so much

I am pleased it worked and many thanks for letting us know it is now OK.

Hi,
Very interesting. When I first saw the code and tried, it worked. Today I integrate it to a project, it didn’t work. I tried the code alone original and again it didn’t work.

I have just tried the script and it worked fine.

More details are required, can you supply the source code that is not working and i will try running the script.

1 Like

I tried the original script you sent. It did work 2 days ago. Now i am trying the same but it doesnt. Funny !

I uploaded the original script you sent to another hosting i see the same it doesnt work :

sometrials.netii.net/searchmain.php

I SOLVED ! I made the notepad ++ coding “UTF-8”. it seems solved now

I am pleased you managed to sort your problem and to show the results. I was wondering why the script was not working.

I will have to make sure I do not make the same mistake :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.