Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation trying to search a string for certain strings and replace them with a new string

    Hi All,

    Ok i am doing some work for my local football team, and i have a db table with players names and another with fixture details.

    Now in the fixture details i have a field for match report which is a long text field type.

    What i am trying to do is create a function which replaces all the instances of each player name found in the report with the players name but hyper linked to his profile page.

    so for example if my name was in the report the function would produce the following
    Luke Jackson => <a href='staff_profile.php?id=10929'>Luke Jackson</a>

    So i tried this
    PHP Code:
    function FindNames($report)
    {
        
    //GET LIST OF NAMES TO SEARCH FOR
        
    $sql mysql_query("SELECT staffID, staffName FROM tbl_staff");
        while(
    $row=mysql_fetch_array($sql))
        {
            
    $id $row['staffID'];
            
    $name $row['staffName'];
            
    $addLink str_replace($name"<a href='staff_profile.php?id=$id'>$name</a>"$report);    
        }
        return 
    $report;

    and tried echoing the match report out like so
    PHP Code:
    <?php echo FindNames($report)?>
    but it doesnt replace any of the names found in the report, nothing changes any ideas what i doing wrong?

    many thanks
    Luke
    Last edited by LJackson; 08-19-2012 at 11:32 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    As you are specifically treating the returned data as an associative array then you should use mysql_fetch_assoc. Or, if you insist,

    PHP Code:
    while ($row=mysql_fetch_array($sqlMYSQL_ASSOC)) 
    The replacements are performed left-to-right. So replacing $name with more text that also includes $name may be causing an issue as well.

    [If this is an issue then one way around this that occurs to me is to replace $name with XXXXXX, then, immediately after, to replace XXXXXX with $name.]

    BTW mysqli is recommended and use of mysql is discouraged. But you are by no means alone in side-stepping this advice?!
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Also, if

    PHP Code:
    <?php echo FindNames($report)?>
    echo's $report in full, but completely unaltered, then this suggests to me that the space between the first and last-name is an issue (as it doesn't find the name(s) at all).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks mate, i also just noticed that im returning

    return $report;

    but the new text with all the replacements is called

    $addLink = str_replace($name, "<a href='staff_profile.php?id=$id'>$name</a>", $report);

    so i renamed $addLink to $report and it works a treat thanks for your help

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    @LJackson
    That was the first thing I noticed (he, he) but I made the incorrect assumption that it was making the replacements to $replace, so assumed that this wasn't the issue. Anyway..

    BTW I still think using mysql_fetch_assoc is more sensible
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •