Dynadot โ€” .com Registration $8.99

PHP/MySQL replacing letters in text

Spaceship Spaceship
Watch

freeflow

Established Member
Impact
13
Hi everyone. I need some help again with PHP and MySQL.

Here is what I would like to do:

// Example retrieved text from DB = Axxxax xaxx. Cxxax xbxxx. Bxxx ....

$text = All letters in $text have been replaced where A=&nnn; a=&mmm; B=&uuu; etc.

mysql_query("UPDATE Ads1 SET Text ='$text' WHERE ID = '$id' ");
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
looks like you want to use string replace, if your altering every letter in a sentence/text then you would probably want to traverse the string as an array and make the new string like this:

PHP:
$newString = '';
        $string = 'aAb';        
        
        foreach (str_split($string) as $char)
        {            
            switch ($char)
            {
                case 'a':
                    $newString .= '&mmm';
                    break;
                case 'A':
                    $newString .= '&nnn';
                    break;
                case 'b':
                    $newString .= '&uuu';
                    break;
            }
        }

        echo $newString;

converting it back from the new string won't be nearly as easy though you would need to add a ending character to each element and the explode off that element and do the reverse mapping.

Cheers,

Jay
 
0
•••
Thanks baxter for your reply. I am still having some problems making it work.

Can you help me with this code:

The code below works fine until I change "Hรคllo World of PHP" to โ€œ$textโ€
with the same text from the DB. when I echo the variable the text appears
without the changed characters. What am I doing wrong?


This works:

$vowels = array("รค");
$onlyconsonants = str_replace($vowels, "รค", "Hรคllo World of PHP");


This doesnโ€™t work:

$vowels = array("รค");
$onlyconsonants = str_replace($vowels, "รค", โ€œ$textโ€);
 
0
•••
$vowels = array("รค");
$onlyconsonants = str_replace($vowels, "รค", โ€œ$textโ€);

$text shouldn't be in quotes. It should be like this:
$onlyconsonants = str_replace($vowels, "รค", $text);

If you are just trying to change those funny letters to their html encoded equivalent (e.g. รค to &auml), you could try the htmlentities() php function - PHP: htmlentities - Manual
 
1
•••
0
•••
No worries. Glad I could help!
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back