NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP/MySQL replacing letters in text

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 07-27-2010, 02:26 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
freeflow's Avatar
Join Date: Dec 2006
Posts: 236
freeflow is on a distinguished road
 



Help! PHP/MySQL replacing letters in text


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' ");
freeflow is offline   Reply With Quote
Old 07-27-2010, 06:07 PM   #2 (permalink)
NamePros Regular
 
baxter's Avatar
Join Date: Apr 2006
Posts: 360
baxter is just really nicebaxter is just really nicebaxter is just really nicebaxter is just really nice
 


Ethan Allen Fund Save The Children
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 Code:
$newString '';
        
$string 'aAb';        
????: NamePros.com http://www.namepros.com/programming/669125-php-mysql-replacing-letters-in-text.html
        
        foreach (
str_split($string) as $char)
????: NamePros.com http://www.namepros.com/showthread.php?t=669125
        {            
            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
__________________
Canadian Domain Registrar Ready.ca
baxter is offline   Reply With Quote
Old 07-28-2010, 03:03 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
freeflow's Avatar
Join Date: Dec 2006
Posts: 236
freeflow is on a distinguished road
 



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”);
freeflow is offline   Reply With Quote
Old 07-28-2010, 04:28 PM   #4 (permalink)
Senior Member
 
musicplace.co.nr's Avatar
Join Date: Oct 2007
Location: Sydney, Australia
Posts: 1,022
musicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud of
 



Alzheimer's
Originally Posted by freeflow View Post

$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
musicplace.co.nr is offline   Reply With Quote
Old 07-31-2010, 07:00 AM THREAD STARTER               #5 (permalink)
NamePros Regular
 
freeflow's Avatar
Join Date: Dec 2006
Posts: 236
freeflow is on a distinguished road
 



Originally Posted by musicplace.co.nr View Post
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
Thanks for the advice! This code does everything I wanted and more.
????: NamePros.com http://www.namepros.com/showthread.php?t=669125
$text=htmlentities($text);
freeflow is offline   Reply With Quote
Old 07-31-2010, 05:55 PM   #6 (permalink)
Senior Member
 
musicplace.co.nr's Avatar
Join Date: Oct 2007
Location: Sydney, Australia
Posts: 1,022
musicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud ofmusicplace.co.nr has much to be proud of
 



Alzheimer's
No worries. Glad I could help!
musicplace.co.nr is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:36 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger