SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing string data with PHP

    I would like to create a function that looks for a name of an attorney within a string and if it finds the name then the function will automatically link it.

    I think the following example makes it clear what I am trying to do.

    PHP Code:
    $attorney_array = array(1=>'Michael Anderson'2=>'Tom A. Atkinson'3=>'Pat M. Boyer');

    $text '<p>Pat M. Boyer started her career as an audiologist in 1981, and has worked in that capacity in major medical centers in the Cleveland area as well as at the university level.</p>';

    function 
    parse_attorney_bio($text$attorney_array){

    }

    echo 
    $text parse_attorney_bio($text$attorney_array);

    // Output
    <p><a href="bio.php?id=3">Pat MBoyer</astarted her career as an audiologist in 1981, and has worked in that capacity in major medical centers in the Cleveland area as well as at the university level.</p>'; 
    Can anyone lead me to some tutorials or give me some tips on how to write such a function?

    Thanks!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here you go, you could quite easily create a function for this...

    PHP Code:
    <?php
    $aAttorneys 
    = array(
        
    1    =>    'Michael Anderson',
        
    2    =>    'Tom A. Atkinson',
        
    3    =>    'Pat M. Boyer'
    );

    $sString 'We have top attorneys such as, Michael Anderson, Tom A. Atkinson & Pat M. Boyer in our employ.';

    foreach(
    $aAttorneys as $iId => $sAttorney)
    {
        
    $sString str_replace(
            
    $sAttorney,
            
    sprintf(
                
    '<a href="bio.php?id=%s">%s</a>',
                
    $iId,
                
    $sAttorney
            
    ),
            
    $sString
        
    );
    }

    echo 
    $sString;
    #We have top attorneys such as, <a href="bio.php?id=1">Michael Anderson</a>, <a href="bio.php?id=2">Tom A. Atkinson</a> & <a href="bio.php?id=3">Pat M. Boyer</a> in our employ.
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome SilverBulletUK! You are good. Thank you very much.


Bookmarks

Posting Permissions

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