www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22

Thread: This preg_replace how do I use it with arrays ?

  1. #16
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Hi, what code are you using to get those results?
    If it's my code causing the error I will gladly look into it.
    JavaScript: Learn | Validate | Compact

  2. #17
    Join Date
    May 2006
    Posts
    245
    This is the code ( including the data )


    PHP Code:
    $input_data = <<<EOD
        NOW This is a special test and is all about John and his friends Johns like John Smith is now to be a jerk. And this is another test to find out what this thing is all about. We need to understand what we can do with this.
        He is John's friend and lives across the road John J. Smith is a good person, but John is living about 1 mile away !!! The end
        EOD;

        
    $input_data = strtolower($input_data);

        
        echo "This is the article:<br><br>
    $input_data<br><br>";
            
        
    $words_reps = array(
            'john' => '{John: 123 Main St}',
            'now' =>  '{now|right now|just now}',
            'is' =>  '{is|really is|most certainly is}',
            'special' =>  '{special|really special|very special}',
            'this' =>  '{this|this thing|this unique}',
            'this is' =>  '{this is|this wretched|this is hell}',
            'john smith' => '{John Smith: 456 Elm St|John B Smith: 456 Elm St}',
            'john j. smith' => '{John J. Smith: 531 Park Ave}',
            'a' => '{a|some|a very|a definite|a specific|a particluar}',
            'about' => '{about|regarding|concerning|with regards to}',
            'test' => '{test|examination|in depth look|research project}',
            'about to' => '{about to|nearly|on the point of|will|just about to}',
            'across the' => '{across the|around the|all over the|over the|through the|round the|all around the}'
            );
        
        echo "This is the FIND AND REPLAY ARRAY:<br>";
        foreach(
    $words_reps as $x=>$x_value) {
            echo 
    $x . "-->" . $x_value;
            echo "<br>";
            } 
        
        echo "<br>======================================================<br>";


        echo "This is the NEW find AND replay ARRAY:<br>";
        
        
            // KRSORT() Sorts an array by key in reverse order, 
            // maintaining key to data correlations

        krsort(
    $words_reps);
        foreach(
    $words_reps as $x=>$x_value) {
            echo 
    $x . "-->" . $x_value;
            echo "<br>";
            } 

        foreach(
    $words_reps as $key => $val) {
            
    $regex = '/(?<!'.preg_quote('XxX', '/').')'.'\b'.preg_quote($key, '/').'\b'.'/i';
            
    $regexData[$regex] = 'XxX'.$val;
            }
            

        // The array_keys() function returns an array containing the keys.
        
    $new_data = preg_replace(array_keys($regexData), $regexData$input_data);
        
    $new_data = str_replace('XxX', '', $new_data);

        echo "<br>This is the RESULT:<br><br>
    $new_data"; 
    I think its what you recommended - but I
    may have made an error somewhere

    Thanks.


    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  3. #18
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    You're using NogDog's code there.
    I have updated the one I submitted anyway for comparison:

    PHP Code:
    <?php

    $input_data 
    = <<<EOD
        NOW This is a special test and is all about John and his friends Johns like John Smith is now to be a jerk. And this is another test to find out what this thing is all about. We need to understand what we can do with this.
        He is John's friend and lives across the road John J. Smith is a good person, but John is living about 1 mile away !!! The end
    EOD;

        
    $words_reps = array(
            
    'john' => '{John: 123 Main St}',
            
    'now' =>  '{now|right now|just now}',
            
    'is' =>  '{is|really is|most certainly is}',
            
    'special' =>  '{special|really special|very special}',
            
    'this' =>  '{this|this thing|this unique}',
            
    'this is' =>  '{this is|this wretched|this is hell}',
            
    'john smith' => '{John Smith: 456 Elm St|John B Smith: 456 Elm St}',
            
    'john j. smith' => '{John J. Smith: 531 Park Ave}',
            
    'a' => '{a|some|a very|a definite|a specific|a particluar}',
            
    'about' => '{about|regarding|concerning|with regards to}',
            
    'test' => '{test|examination|in depth look|research project}',
            
    'about to' => '{about to|nearly|on the point of|will|just about to}',
            
    'across the' => '{across the|around the|all over the|over the|through the|round the|all around the}'
        
    );

        
    $sets = array();
        foreach (
    $words_reps as $i => $j) {
            
    $sets[count(explode(' '$i))][$i] = $j;
        }
        
    krsort($setsSORT_NUMERIC);

        
    $words = array();
        foreach (
    $sets as $set) {foreach ($set as $word => $replace) {
            
    $word_index '{WORD_' count($words) . '}';
            
    $input_data preg_replace('/\b' preg_quote($word'/') . '\b/'$word_index$input_data);
            
    $words[$word_index] = $replace;
        }}

        foreach (
    $words as $i => $j) {
            
    $input_data str_replace($i$j$input_data);
        }

        echo 
    '<p>' $input_data '</p>';

    ?>
    Output

    Code:
    NOW This {is|really is|most certainly is} {a|some|a very|a definite|a specific|a particluar} {special|really special|very special} {test|examination|in depth look|research project} and {is|really is|most certainly is} all {about|regarding|concerning|with regards to} John and his friends Johns like John Smith {is|really is|most certainly is} {now|right now|just now} to be {a|some|a very|a definite|a specific|a particluar} jerk. And {this is|this wretched|this is hell} another {test|examination|in depth look|research project} to find out what {this|this thing|this unique} thing {is|really is|most certainly is} all {about|regarding|concerning|with regards to}. We need to understand what we can do with {this|this thing|this unique}. He {is|really is|most certainly is} John's friend and lives {across the|around the|all over the|over the|through the|round the|all around the} road John J. Smith {is|really is|most certainly is} {a|some|a very|a definite|a specific|a particluar} good person, but John {is|really is|most certainly is} living {about|regarding|concerning|with regards to} 1 mile away !!! The end
    JavaScript: Learn | Validate | Compact

  4. #19
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Note that when you use the HEREDOC method of quoting (with "<<<"), the string that ends the quote ("EOD;" in this example) must not have any leading tabs/spaces before it (as corrected in bionoid's update).
    "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. #20
    Join Date
    May 2006
    Posts
    245
    You are both very kind.
    Thanks very much for your help.

    Will work on this tomorrow.
    Hopefully get it going

    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  6. #21
    Join Date
    May 2006
    Posts
    245
    OK -

    thanks for sharing that

    It seems to be doing what I need - the addresses are coming out better.

    I will need to work through and comment the code to understand it !!

    Thanks again.


    .
    Last edited by jeddik; 12-05-2013 at 01:42 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  7. #22
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Yeah, I think previously I didn't need to do a case-insensitive replace, but looking at the new data and results I've just turned that on, sorry.

    PHP Code:
    <?php

    $input_data 
    = <<<EOD
        NOW This is a special test and is all about John and his friends Johns like John Smith is now to be a jerk. And this is another test to find out what this thing is all about. We need to understand what we can do with this.
        He is John's friend and lives across the road John J. Smith is a good person, but John is living about 1 mile away !!! The end
    EOD;

        
    $words_reps = array(
            
    'john' => '{John: 123 Main St}',
            
    'now' =>  '{now|right now|just now}',
            
    'is' =>  '{is|really is|most certainly is}',
            
    'special' =>  '{special|really special|very special}',
            
    'this' =>  '{this|this thing|this unique}',
            
    'this is' =>  '{this is|this wretched|this is hell}',
            
    'john smith' => '{John Smith: 456 Elm St|John B Smith: 456 Elm St}',
            
    'john j. smith' => '{John J. Smith: 531 Park Ave}',
            
    'a' => '{a|some|a very|a definite|a specific|a particluar}',
            
    'about' => '{about|regarding|concerning|with regards to}',
            
    'test' => '{test|examination|in depth look|research project}',
            
    'about to' => '{about to|nearly|on the point of|will|just about to}',
            
    'across the' => '{across the|around the|all over the|over the|through the|round the|all around the}'
        
    );

        
    $sets = array();
        foreach (
    $words_reps as $i => $j) {
            
    $sets[count(explode(' '$i))][$i] = $j;
        }
        
    krsort($setsSORT_NUMERIC);

        
    $words = array();
        foreach (
    $sets as $set) {foreach ($set as $word => $replace) {
            
    $word_index '{WORD_' count($words) . '}';
            
    $input_data preg_replace('/\b' preg_quote($word'/') . '\b/i'$word_index$input_data);
            
    $words[$word_index] = $replace;
        }}

        foreach (
    $words as $i => $j) {
            
    $input_data str_replace($i$j$input_data);
        }

        echo 
    '<p>' $input_data '</p>';

    ?>
    Output

    {now|right now|just now} {this is|this wretched|this is hell} {a|some|a very|a definite|a specific|a particluar} {special|really special|very special} {test|examination|in depth look|research project} and {is|really is|most certainly is} all {about|regarding|concerning|with regards to} {John: 123 Main St} and his friends Johns like {John Smith: 456 Elm St|John B Smith: 456 Elm St} {is|really is|most certainly is} {now|right now|just now} to be {a|some|a very|a definite|a specific|a particluar} jerk. And {this is|this wretched|this is hell} another {test|examination|in depth look|research project} to find out what {this|this thing|this unique} thing {is|really is|most certainly is} all {about|regarding|concerning|with regards to}. We need to understand what we can do with {this|this thing|this unique}. He {is|really is|most certainly is} {John: 123 Main St}'s friend and lives {across the|around the|all over the|over the|through the|round the|all around the} road {John J. Smith: 531 Park Ave} {is|really is|most certainly is} {a|some|a very|a definite|a specific|a particluar} good person, but {John: 123 Main St} {is|really is|most certainly is} living {about|regarding|concerning|with regards to} 1 mile away !!! The end
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

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

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