View Full Version : str_replace/eregi_replace in msg // looped.
Harlequin
01-22-2004, 11:57 AM
Since Xenon and NTLDR helped me with the SQL searching problem (again, thankies much guys. :)) I've been attempting to play with foreach and while.
I'm already aware of eregi_replace and str_replace and they work beautifully, however for the life of me I can't seem to get this replacement loop down.
I'd like for $message and str_replace (or eregi_replace) with a SQL table called $cc to filter and output the loop.
The $cc table contains an auto-incrementing field called tagid.
The term to be searched for in $message is $cc[tags]. It's replacement is $cc[tagsname].
Again, it's not the eregi or str_replace that's killing me.. this example works:
$message = eregi_replace("$cc[tags]","$cc[tagsname]",$message);
But the loop for this to keep reading through each $cc[tags] has me tied. The foreach function I had came out with a big goose egg -- 0, and while wanted to loop continuously.
Any suggestions?
NTLDR
01-22-2004, 12:12 PM
I'm a little confussed on what you are trying to do. Firstly I assume that $cc['tag'] and $cc['tagname'] are variables with a value assigned? If $message is just a string then it should replace all instances of $cc['tag'] within the string, you shouldn't need to loop.
Harlequin
01-22-2004, 03:58 PM
That's the thing. I thought that was true, too, and because of that I'm confused out of my wits. But lemme see if I can explain the situation a little better to see if there's any way to go about this in the way it's currently setup. If not I'll try a different method.
This is how the db looks, per field: (layout: id#, tag to search for, output in msg)
1, -review-, This is the review tag.
2, -reviewimg-, This is the reviewimg tag.
$cc[tagid], $cc[tags], $cc[tagsname]
All of this information can be added/removed from the admincp. I've created a menu that does so.
So here we are, two tags that should be replaced. FYI, The desired place of outcome is the postbit if this helps.
So on viewing the postbit with the eregi'd/str_replace'd entries.. only the first tag replaced itself.
-review-, output: This is the review tag.
-reviewimg-, output: -reviewimg-.
One tag does replace, but the other doesn't. And that's why I was assuming there'd have to be some sort of a loop involved. :)
g-force2k2
01-23-2004, 02:05 AM
Harlequin try this for the loop.
$tags = $DB_site->query ( "SELECT * FROM TABLE_NAME_HERE " ) ;
while ( $tag = $DB_site->fetch_array ( $tags ) ) :
$message = eregi_replace ( $tag['tags'], $tag['tagsname'], $message ) ;
endwhile ;
You just have to edit TABLE_NAME_HERE with the table name that you're selecting the tag data from, hope that helps.
Cheers,
g-force2k2
Harlequin
01-23-2004, 02:56 AM
Thank you for the solution, g-force2k2. :) I don't know why I didn't think of it sooner.
I've been coding on another hack that uses this solution [aside from the journal hack located in the signature]. What kills me is my version of the journal hack has several instances that I've even coded in myself for similiar functions and I still came here to ask this question. Am I the only one that idioticly does this? :) Please tell me I'm not the only person who doesn't have all the screws tightened with the elevator not quite reaching the top floor. :P
Does it help matters that I work on code until the wee hours of the morning whenever people should be in bed and I'm still staring at the screen like a zombie?
I think I need to catch more z's. x.x
Anyway, again, thank you NTLDR and g-force2k2. Your time is much appreciated. :)
Harlequin
01-27-2004, 07:34 AM
<< moved to a new thread -- off-topic to this thread. >>
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.