Jump to content

[SOLVED] Searching for key words in a string


eoeowner

Recommended Posts

Okay, here's the deal. I have a php based site that shows information about different bands. I want to be able to link to a particular band by clicking on a word within the entered variable for the text.

 

eg: Listen to Tool, they are a top band. - I want the word Tool to be a link to the Tool page.

 

I have worked out by exploding the string, checking each word against the band names and then joining the string back together, I can find any one word key words in any variable....

 

But, I want to be able to have a phrase. So, I want to be able to find "Iron Maiden" or "Cradle of Filth" and that doesn't work with the join/explode thing since they need a delimiter.

 

Apart from some sort of sadistic SELECT search.... how can I do this???

 

Link to comment
Share on other sites

Okay, I've had a decent go at trying to get it and it's beating me...

 

If i can give you an exact example, can you help me out with the preg_ code?

 

So...

 

$string="I love Iron Maiden they are <b>sooo cool</b>"

 

$keyword="Iron Maiden"

 

$KeywordLinkNumber="34"

 

Ultimately I want to have $string to read...

 

$string="I love <a href="example.php?34">Iron Maiden</a> they are <b>sooo cool</b>"  (with slashes if need be)

 

How would you work that?

 

The idea is that the keywords and link numbers are kept in a table and I want to check any posts for links to bands. So that when a post is written ($string) it automatically inserts a link to the band's page.

 

How would I go about doing that?  ???

Link to comment
Share on other sites

You could do something like this:

<?php
$keywords = array(34 => 'Iron Maiden');
$string = 'I love Iron Maiden they are sooo cool';
foreach ($keywords as $num => $key)
    $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string);
?>

 

Ken

Link to comment
Share on other sites

Okay, that seems to be working okay....

 

A couple of questions, though.

 

Instead of $keywords = array (34 => 'Iron Maiden');

 

I need to pull a query from the mySQL db how do I translate $keyword and $num into the array ($num => $key) format? Does it change it much?

 

Also, can I make it case insensitive?

 

Thanks for your help btw....  :)

Link to comment
Share on other sites

Okay I have...

 

<?php

$num[1]=34;
$num[2]=32;
$key[1]="Iron Maiden";
$key[2]="cool";

$keywords = array($num[$a] => $key[$a],$num[2] => $key[2]);


$string = 'I love Iron Maiden they are sooo cool and iron maideny';

foreach ($keywords as $num => $key)
    $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string);

print $string;

?>

 

...working for me, but how do I take the next step and automate the population of the $keywords array?

Link to comment
Share on other sites

You don't need the extra arrays. Change your example to:

<?php
$keywords = array(34 => "Iron Maiden",32 => 'cool');


$string = 'I love Iron Maiden they are sooo cool and iron maideny';

foreach ($keywords as $num => $key)
    $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string);

print $string;

?>

 

If you need to automagically add to this array using a database. do something like:

<?php
//
// your mysql query here
//
while ($rw = mysql_fetch_assoc($result)) 
    $keywords[$rw['num']] = $rw['keyword'];
?>

 

Or just do the replace in the database while loop:

<?php
//
// your mysql query here
//
while ($rw = mysql_fetch_assoc($result)) 
    $string = str_replace($rw['keyword'],'<a href="example.php?' . $rw['num] . '">' . $key . '</a>',$string);
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.