Hello folks,

I built an article manager on my website. What I am trying to do is in the output of that news, in the field 'story' do a find and replace on some items.

For example, if the story mentions John Smith, I'd like for the PHP to automatically search for John Smith and if it finds him link to his bio page.

So, John Smith would be replaced with <a href="bio.php?staffID=$staffID">John Smith</a>

Any ideas?

    If you know what items to search for, that string search and replace should be easy enough. The question is, how does the script know that it should search for 'John Smith' with a given staffID in the first place?

      I guess I could replace:

      "John Smith"

      with:

      <a href="bio.php?staffID=1">John Smith</a>

      How do I do this string search and replace in the field '$story'

        $name = "John Smith";
        $id = 1;
        $name_link = '<a href="bio.php?staffID='.$id.'">'.$name.'</a>';
        $story = str_replace($name, $name_link, $story);
        
          esukf wrote:
          $name = "John Smith";
          $id = 1;
          $name_link = '<a href="bio.php?staffID='.$id.'">'.$name.'</a>';
          $story = str_replace($name, $name_link, $story);
          

          works perfect. if i want to add multiple people do I have to do:

          $name = "John Smith";
          $id = 1;
          $name_link = '<a href="bio.php?staffID='.$id.'">'.$name.'</a>';
          $story = str_replace($name, $name_link, $story);
          
          $name = "Aaron Jones";
          $id = 2;
          $name_link = '<a href="bio.php?staffID='.$id.'">'.$name.'</a>';
          $story = str_replace($name, $name_link, $story);
          

          or can I do:

          $name = "John Smith";
          $id = 1;
          $name = "Aaron Jones";
          $id = 2;
          $name_link = '<a href="bio.php?staffID='.$id.'">'.$name.'</a>';
          $story = str_replace($name, $name_link, $story);
          

            Great, it's working. I made a seperate .php file and have an include command in the page I want the content to go in...

            Real quick, is there anyway that I can set something up so that it lists, in the format needed, automatically from the database?

            So if I add a person to the DB, I don't have to add him/her to the .php list for the string replacement as well?

              Write a Reply...