PHP/MySQL replacing letters in text

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
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
•••
Olitt — high-converting AI websites, only from $1/moOlitt — high-converting AI websites, only from $1/mo

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com
Catchy
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back