www.webdeveloper.com
Results 1 to 6 of 6

Thread: How can I do a more detailed find and replace ?

  1. #1
    Join Date
    May 2006
    Posts
    245

    How can I do a more detailed find and replace ?

    Hi,

    I am have been using a basic str_replace()
    when swapping out data, but now it has good a bit more complicated.

    My data is in a mysql table and looks like:

    column: name
    Dave
    John
    Dave Brown
    Steve
    Dave H. Brown
    John Graves
    Dave Griffins Brown


    column: address
    Dave:56, Google Ave
    John:77 Jognst Road
    The Beach hut owned by Dave Brown
    Dave H. Brown:55 Frisbie Park Avenue
    Steve:2 Giant Pea Garden
    John Graves:5627 Ant Hill Rise
    Dave Griffins Brown:25 Franks Drive

    My code reads the table and creates arrays:

    PHP Code:
    while ($row mysql_fetch_assoc($result)) {    
      
    extract($row);
      
    $names[] = $name.' ';    
      
    $addresses[] = $address.' ';    
      }
    $new_data str_ireplace($names,$addresses,$input_data); 
    The $input_data looks like:
    "The fireman Dave did something. Then John Graves did something else
    later on Dave Brown came over with Steve The following day police officer Dave Griffins Brown
    was found dead by John";

    ( But of course much longer)

    Clearly if I do the basic str_ireplace() things are going to get mixed up because there
    are 3 names starting with "Dave"

    I guess I may have to abandon the str_ireplace() and perhaps sort the arrays and
    then step through doing a comparison.
    ( or is there a clever way to use str_ireplace() to do this )

    I am not at all sure what is the best way to do this, so would really appreciate your help.

    open to suggestions

    Thanks.
    Last edited by jeddik; 11-16-2013 at 05:46 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  2. #2
    Join Date
    May 2006
    Posts
    245
    Looks like I needs to be checking the words in front of the the current word ...

    Example:
    When the script sees "John" in the $input_data,
    It needs to look ahead to see what comes after John
    - look at the next 2 words.
    Then check to see if those 3 words are in the $names array.
    If it is , do the replace ...

    If not found see if John plus 1 word is found in the $names array.
    If it is found, do the replace ...

    If not found see if just John is found in the $names array.
    If it is found, do the replace ...

    If not move to the next word.



    .
    Last edited by jeddik; 11-17-2013 at 02:47 PM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  3. #3
    Join Date
    May 2006
    Posts
    245


    No suggestions ???

    Please help - I am stuck


    .
    Last edited by jeddik; 11-18-2013 at 10:25 PM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    Only thing I can think of would be something using preg_replace(), and probably require at least two passes through the text.

    Get all the strings to be searched for, sorted descending (so that that "John Smith" will sort before "John").

    On the first pass, add some sort of marker at the start of the replacement. Let's use "{marker}". This will allow us to do a negative look-behind assertion, so that we don't replace something that's already been matched. So, you would add "{marker}" to the beginning of each replacement string. Then you'd have to convert your search strings into the desired regex, perhaps:
    PHP Code:
    foreach($names as &$value) {
        
    $value '/(?<!'.preg_quote('{marker}').')'.preg_quote($value).'/i';
    }
    // then...
    $new_data preg_replace($names,$addresses,$input_data);
    // then get rid of the markers:
    $new_data str_replace('{marker}'''$new_data
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2006
    Posts
    245
    OK

    I am a bit new to these look forward and look behind
    ( but I read the manual about it )

    I have changed the mysql table to use the more general terms
    "word" and "synonym"

    So I tried this:

    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
            
    extract($row);    
            
    $words[] = $word.' ';
            
    $synonyms[] = $synonym.' ';
            }
            
        
    arsort($words);

        foreach(
    $words as $value) { 
            echo 
    "$value<br>"
            } 
            
        foreach(
    $words as &$value) { 
            
    $value "/(?<!".preg_quote('{marker}').")".preg_quote($value)."/i"
            } 

        foreach(
    $words as $value) { 
            echo 
    "$value<br>"
            } 
    My Output is:

    your work
    you can
    you also have
    writing
    words
    with
    who has
    which i
    we offer
    way in which
    was born
    very small
    very
    traveling to
    ...

    then:

    /(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/(?/

    For some reason the value is not getting properly changed ??



    PS I tried adding a deliminator to the preg_quote like this:

    PHP Code:
    foreach($words as &$value) { 
        
    $value "/(?<!".preg_quote('{marker}''/').")".preg_quote($value'/')."/i"

    But got the same result


    ALSO TRIED SINGLE QOTES:

    PHP Code:
    foreach($words as &$value) { 
        
    $value '/(?<!'.preg_quote('{marker}''/').')'.preg_quote($value'/').'/i'



    Same thing ...




    .
    Last edited by jeddik; 11-19-2013 at 02:04 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,357
    This seems to work, if you want to dig through it and see if I inadvertently fixed something (and do some more thorough testing).

    PHP Code:
    <?php

    $searchData 
    = array(
        
    'John' => 'John: 123 Main St',
        
    'John Smith' => 'John Smith: 456 Elm St',
        
    'John J. Smith' => 'John J. Smith: 531 Park Ave'
    );
    krsort($searchData);
    print_r($searchData);
    $regexData = array();
    foreach(
    $searchData as $key => $val) {
        
    $regex '/(?<!'.preg_quote('{marker}''/').')'.preg_quote($key'/').'/i';
        
    $regexData[$regex] = '{marker}'.$val;
    }
    print_r($regexData);

    $text = <<<EOD
    Testing 1, 2, 3. John Smith is a jerk. The end
    EOD;

    $result preg_replace(array_keys($regexData), $regexData$text);
    $result str_replace('{marker}'''$result);
    echo 
    $result;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles