Jump to content

Select a list of words in a DB and replace them if present in a text


anarchoi

Recommended Posts

Ok i have this code that selects a list of words in a row of my db:

$query  = "SELECT title FROM zine_pages_categories";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
    echo $row["title"]."<br />";
}


what i'd like to do now, say i have a text called $text, i want to replace all values found in "TITLE" in my $text by a link (where the link name is the corresponding "title")

i could use a preg_replace() to do that but problem is that there is more than 1 result in "title" and i'm not an experienced programmer

any help?
Ok i have a text with a lot of words...

And i have a table in my database containing "title" which is a lot of names...

Say i have Peter Kropotkine and Joseph Proudhon in my database

And say my text with a lot of words is $text



i'd like to replace ALL words "Peter Kropotkine" and "Joseph Proudhon" in $text by something like:
<a href=\"mypage.php?name=Peter%20Kropotkine\">Peter Kropotkine</a>

I can do that with a preg_replace() but there is not ONLY Peter Kropotkine in my database, i got a few others 50 names to check.
you could do something like this:

ex

[code=php:0]<?php


$text = "Here´s your string with a lot of text and
a few names like Peter Kropotkine and Joseph Proudhon";


include("db_connection.php");

$sql = "SELECT * FROM your_table";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)) {

$name = $row['name']; // name from your database
$text = preg_replace("/($name)/ise", '"<a href=\"mypage.php?name=" . urlencode("\\1") . "\">\\1</a>"', $text);

}


echo $text;


?>[/code]
well all i did was replacing db variables in your code by phpnuke's db variables



$textx = "Here´s your string with a lot of text and a few names like Peter Kropotkine and Joseph Proudhon bla bla KROPOTKINE, Pierre bla bla";


$result = $db->sql_query("SELECT * FROM ".$prefix."_pages_categories");

while($row = mysql_fetch_assoc($result)) {


$name = $row['title']; // name from your database


$textx = preg_replace("/($name)/ise", '"<a href=\"mypage.php?name=" . urlencode("\\1") . "\">\\1</a>"', $textx);

echo "$textx";

}
Try this:

[code]
<?php
$text = 'Here is your string with a lot of text
and a few names like Peter Kropotkine and Joseph Proudhon';
$names = array();
$names[] = 'Joseph Proudhon';
$names[] = 'Peter Kropotkine';
foreach($names as $name){
  $text = str_replace($name,'<a href="mypage.php?name=' . str_replace(' ', '%20',$name) .  '">' . $name . '<' . '/a' . '>', $text);
}
echo $text;

?>
[/code]

BTW--you can combine this part into ONE string.  I seperated it out so that the board wouldn't convert it to bbCode.
[b]'<' . '/a' . '>'[/b]

This is the HTML code that's sent to my browser when I ran this.
[quote]
Here is your string with a lot of text
and a few names like <a href="mypage.php?name=Peter%20Kropotkine">Peter Kropotkine</a> and <a href="mypage.php?name=Joseph%20Proudhon">Joseph Proudhon</a>
[/quote]
hmmm any way you can make it look for field 'title' in table 'zine_pages_categories' to automaticaly add all results found 'title' in $names[]


$names = array();
$names[] = 'Joseph Proudhon';
$names[] = 'Peter Kropotkine';


i'd need something that would replace 'Joseph Proudhon' and 'Peter Kropotkine' by each entries in 'title'
Sure.

[code]
$names = array();

$result = $db->sql_query("SELECT * FROM ".$prefix."_pages_categories");

while($row = mysql_fetch_assoc($result)) {

// creates an array $names, where the names from $row['title'] are stored 
$names[] = $row['title'];

}

// replace each name with link
foreach($names as $name){
  $text = str_replace($name,'<a href="mypage.php?name=' . str_replace(' ', '%20',$name) .  '">' . $name . '<' . '/a' . '>', $text);
}
echo $text;
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.