Current location: Hot Scripts Forums » Programming Languages » PHP » replacing...


replacing...

Reply
  #1 (permalink)  
Old 04-19-06, 02:35 AM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Question replacing...

hi, help me please....

PHP Code:

/***** smilies *****/
$smilies = array(
    
':)' => 'smile.gif',
    
':(' => 'sad.gif',
    
';)' => 'wink.gif'
);


/***** view data *****/
$query mysql_query("SELECT * FROM table ORDER BY date DESC");
while(
$data mysql_num_rows($query)) {
    
$view.= "name: ".$data[1]."<br />";
    
$view.= "subject: ".$data[2]."<br />";
    
    
// i want to use $smilies below
    // :) replaced with <img src="smile.gif" />
    // and more smilies...
    // how to do it?
    
$view.= "message: ".$data[3];

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-19-06, 03:03 AM
Wuiqed Wuiqed is offline
Wannabe Coder
 
Join Date: Aug 2004
Location: Sverige
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

// smilies

$smilies = array(
    
':)'
    
':('
    
';)'
);

//html replacement
$paths   = array(
    
'<img src="smile.gif" alt="" />'
    
'<img src="sad.gif" alt="" />'
    
'<img src="wink.gif" alt="" />'
);

//notice that I changed your mysql_num_rows() to mysql_fetch_array()
//this is because mysql_num_rows() only returns HOW MANY rows was
//fetched but doesn't return the actual rows
$query mysql_query("SELECT * FROM table ORDER BY date DESC");
while(
$data mysql_fetch_array($query)) {
    
$view.= "name: ".$data[1]."<br />";
    
$view.= "subject: ".$data[2]."<br />";
    
    
str_replace($smilies$paths$data[3]);
    
$view.= "message: ".$data[3];


Last edited by Wuiqed; 04-19-06 at 03:09 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-19-06, 03:08 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
^^ Either this or that:

PHP Code:

$smilies = array(

    
':)' => '<img src="smile.gif" alt="" />'
    
':(' => '<img src="sad.gif" alt="" />'
    
';)' => '<img src="wink.gif" alt="" />'
                
);

$query mysql_query("SELECT * FROM table ORDER BY date DESC");
while(
$data mysql_fetch_row($query)) {
    
$view.= "name: ".$data[1]."<br />";
    
$view.= "subject: ".$data[2]."<br />";
    
    
str_replace(array_keys($smilies), array_values($smilies), $data[3]);
    
$view.= "message: ".$data[3];

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-19-06, 03:09 AM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
hi, thanks. i've try the script before. but it's not effective. more effective way?

Quote:
Originally Posted by Wuiqed
PHP Code:

$smilies = array(
    
':)'
    
':('
    
';)'
);

$paths   = array(
    
'<img src="smile.gif" alt="" />'
    
'<img src="sad.gif" alt="" />'
    
'<img src="wink.gif" alt="" />'
);

$query mysql_query("SELECT * FROM table ORDER BY date DESC");
while(
$data mysql_fetch_array($query)) {
    
$view.= "name: ".$data[1]."<br />";
    
$view.= "subject: ".$data[2]."<br />";
    
    
str_replace($smilies$paths$data[3]);
    
$view.= "message: ".$data[3];

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-19-06, 03:13 AM
Wuiqed Wuiqed is offline
Wannabe Coder
 
Join Date: Aug 2004
Location: Sverige
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Not effective? How did you come to that conclusion?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-19-06, 03:34 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Actually, both of the above will work with a minor correction. The str_replace does not modify the source, but instead returns a result. The following is needed -
PHP Code:

    $data[3] = str_replace... 

Here is a third way that keeps the original form of the $smilies array -
PHP Code:

    foreach ($smilies as $key => $value) {

        
$data[3] = str_replace($key"<img src='$value' />"$data[3]);
    } 
Yah I know, it could probably use the addition of the alt text...
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 04-19-06 at 03:40 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-19-06, 03:51 AM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
thanks.... :)

hi, thanks friends......
it's works....
i've modified ....

thanks to nico_swd for:
Quote:
str_replace(array_keys($smilies), array_values($smilies), $data[3]);
thanks to Wuiqed for:
Quote:
//notice that I changed your mysql_num_rows() to mysql_fetch_array()
//this is because mysql_num_rows() only returns HOW MANY rows was
//fetched but doesn't return the actual rows
now the script like below:

PHP Code:

@mysql_pconnect('localhost','root','root');
@
mysql_select_db('database');

$smilies = array(
    
':)' => '<img src="images/smile.gif" />'
    
':(' => '<img src="images/sad.gif" />'
    
';)' => '<img src="images/wink.gif" />'
);

$query mysql_query("SELECT * FROM table");
while(
$data mysql_fetch_array($query)) {
    
$view.= "name: ".$data[1]."<br />";
    
$view.= "subject: ".$data[2]."<br />";
    
    
//;
    
$view.= "message: ".str_replace(array_keys($smilies), array_values($smilies), $data[3])."<hr />";
}

echo 
$view

Last edited by qie; 04-19-06 at 03:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing a form with another sinstone Windows .NET Programming 5 10-28-05 11:59 PM
Replacing every second instance in a string? kdlklm PHP 6 10-19-05 03:19 PM
Help replacing form's title bar zigzag Windows .NET Programming 1 07-05-04 01:00 PM
replacing new lines with spaces PaPPy Visual Basic 3 05-13-04 09:01 PM
replacing certain parts of certain strings in text files Archbob PHP 0 07-29-03 05:23 PM


All times are GMT -5. The time now is 11:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.