 |
boardom b2 message board
|
View previous topic :: View next topic |
Author |
Message |
Cyberian75
Joined: 26 Sep 2002 Posts: 983 Location: Washington
|
Posted: Mon Nov 03, 2003 4:26 am Post subject: PHP Script -- Replace Strings in Posts |
|
|
I wanted to change some of my smilies but doing so would have "corrupted" my older entries with broken links or plain smilie strings. So I did a query via phpMyAdmin to find out how many entries had that particular smilie, and the query returned 119 entries. There was no way I was going to update all of them manually one by one, so I wrote the following script.
To use this script, save it in a file with php extension and fill in "replace" and "with" variables with strings you want to search for and replace with.
Code: |
<?php
require_once("b2config.php");
require_once("b2-include/b2functions.php");
//fill in these variables
$replace = "";
$with = "";
dbconnect();
$query = "SELECT ID, post_content FROM $tableposts WHERE post_content LIKE '%$replace%' ORDER BY post_date DESC";
$result = mysql_query($query) or die(mysql_error());
$numrows = mysql_num_rows($result);
echo "Searching for: ".$replace."<br>";
echo "Replacing with: ".$with."<hr>";
if (!empty($replace) && !empty($with)) {
while ($row = mysql_fetch_object($result)) {
$ID = $row->ID;
$content = stripslashes($row->post_content);
echo "<b>Original Text:</b><br><br>";
echo $content;
echo "<br><br><br>";
$content = str_replace($replace, $with, $content);
echo "<b>New Text:</b><br><br>";
echo $content;
echo "<hr>";
$content = addslashes($content);
$newquery = "UPDATE $tableposts SET post_content = \"$content\" WHERE ID = $ID";
$newresult = mysql_query($newquery) or die(mysql_error());
}
echo "<br><br>".$numrows." record(s) have been updated!";
}
else {
echo "Please specify strings to search and replace.";
}
?>
|
I hope this will be useful to someone... _________________ Michael P.
Last edited by Cyberian75 on Mon Nov 03, 2003 7:57 pm; edited 5 times in total |
|
Back to top |
|
 |
epolady
Joined: 30 Jul 2002 Posts: 800 Location: Texas
|
Posted: Mon Nov 03, 2003 5:46 am Post subject: |
|
|
Great hack! I don't need to use it right now, but I may soon.
Good job! *thumbs up* _________________ No more support from me. Goodbye!
Go upgrade to WordPress, you'll find better support there. |
|
Back to top |
|
 |
Viper007Bond
Joined: 15 Aug 2003 Posts: 263 Location: Portland, Oregon, USA
|
Posted: Mon Nov 03, 2003 10:24 am Post subject: |
|
|
Ditto. I'll probably get rid of all of the <br> tags or something.  _________________ http://www.viper007bond.com
If you haven't already installed b2, I advise you look into WordPress or b2evo instead as b2 is dead. |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 983 Location: Washington
|
Posted: Mon Nov 03, 2003 8:10 pm Post subject: |
|
|
Thanks, guys!
Updated.  _________________ Michael P. |
|
Back to top |
|
 |
Aoidas
Joined: 07 Nov 2003 Posts: 5
|
Posted: Sun Nov 09, 2003 2:23 pm Post subject: Replace letters |
|
|
Hello,
sorry, maybe I don't understand the meaning of this script, but I want't script, which could replace letters with them HTML codes
I need replacing only 9 characters, to my language HTML codes (ą č, and some other).
So, can I use this script somehow?
Sorry, I'm new to PHP, and I don't understand, could this code replace only one character, or some to some other different.
Could you be so helpful, and sohw me how-to do this thing? Thank you. |
|
Back to top |
|
 |
GamerZ
Joined: 15 May 2002 Posts: 536 Location: Singapore
|
Posted: Sun Nov 09, 2003 4:39 pm Post subject: |
|
|
wanted to create it...since u got it first, must as well use urs. =D cheers dude _________________
++ GamerZ.Per.Sg - Complex Simplicity |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 983 Location: Washington
|
Posted: Sun Nov 09, 2003 5:20 pm Post subject: |
|
|
Aoidas,
I wouldn't recommend using the script to replace a single character since it uses wildcards in the search. If you do this, it could replace every instance of that character in your database. However, you could accomplish what you want to do by searching for "<a", not just "a", and replacing it with its character set ("<a").
Don't forget to backup your database first. _________________ Michael P. |
|
Back to top |
|
 |
Aoidas
Joined: 07 Nov 2003 Posts: 5
|
Posted: Tue Nov 11, 2003 3:11 pm Post subject: |
|
|
Cyberian75:
yes, if I would like to replace one of QWERTY letters, it could be a problem with HTML commands, but I I'm going to try only the letters from my language.
Letters: here is keyboard layout. Letters, which I would like to replace is shown in the upper line (from "`" to "\").
About script:
as I understand it, I should fill one symbol in $replace = "symbol_place", and other symbol, that should be placed instead $with = "another_symbol_place"?
So it is only for one symbol. And should I use some of those scripts, or is there any syntax, that I could use to replace some symbols?
And sorry, maybe I'm wrong, and this script only make replaces in all DB, not only in the concrete file (not only in new message as I want)? |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 983 Location: Washington
|
Posted: Tue Nov 11, 2003 5:36 pm Post subject: |
|
|
As far as I know, there are no other ways to search and replace strings than to use some kind of scripts. It's certainly not possible to do this within phpMyAdmin. And yes, this script is only for the database and does not handle actual files or anything outside the database.
The variable "replace" is for strings you want to search for and the other variable "with" is for strings you want that is going to replace the strings you put in for the first variable "replace." Each variable can be assigned multiple words or letters separated by spaces. _________________ Michael P. |
|
Back to top |
|
 |
Aoidas
Joined: 07 Nov 2003 Posts: 5
|
Posted: Thu Nov 13, 2003 1:08 pm Post subject: |
|
|
Thank you, Cyberian75.
I have an idea, that I can replace characters using such code in b2functions.php (b2/include/b2functions.php):
function aa($content) {
$content = preg_replace("/ą/"here is letter, $content);
$content = preg_replace("ą"here is letter html code, $content);
$content = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);
return($content);
}
function unaa($content) {
$content = preg_replace("ą"here is letter html code, "/ą/"here is letter, $content);
return($content);
}
I used autobrize and unautobrize functions. But I don't know, what does last line in function aa, I mean: preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);.
So if anyone could help me, and tell something useful about this code I would be very thankful.
And maybe something is here, but I really have no ideas how to use this in editing comments (so that this function could replace characters with other characters, like pedroivo at linuxgoias dot net example). Maybe it could be somehow transfered to b2 message board.?.
And sorry, for posting here, maybe it's time for new post? But I still post it here, because it's concerned with strings replacing (I hope so.. ) |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 983 Location: Washington
|
Posted: Thu Nov 13, 2003 6:51 pm Post subject: |
|
|
Aoidas wrote: |
I used autobrize and unautobrize functions. But I don't know, what does last line in function aa, I mean: preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);.
|
I believe it replace return characters with breaks "<br />". _________________ Michael P. |
|
Back to top |
|
 |
Aoidas
Joined: 07 Nov 2003 Posts: 5
|
Posted: Fri Nov 28, 2003 10:25 am Post subject: |
|
|
I have some other things to do, and now I'm back to this letters converting.
I founded, that I'm trying to do same thing once again:
in b2-include folder there is file b2vars.php which is used to replace BBCode`s to HTML, so I think, that I could add there required letters and everything should be allright, but one problem:
then I add "ą" (ą), and add in OUT array code ą
Code: |
$b2_bbcode['in'] = array(
'#\ą\#is',
);
$b2_bbcode['out'] = array(
'ą',
);
|
in my blog I get ¹ symbol (^1 - superscript one), and I don't know where is the error.
Well, then I save file and exit, "ą" (ą) converts to "?" (question mark), and this could be because of ANSI encoding table, but I can't save file using UNICODE, because then all the page turns into symbols jumble.
So if anyone have ideas how-to do this correctly - I would be verry thankful. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2 © 2001, 2002 phpBB Group
|