Reputation: 699
i need to remove the next word of search string.. i have search array like array('aa','bb','é');
This is my paragraph 'Hello, this is a test paragraph aa 123 test bb 456'.
In this paragraph i need to remove 123 and 456.
$pattern = "/\bé\b/i";
$check_string = preg_match($pattern,'Hello, this is a test paragraph aa 123 test é 456');
How to get the next word?? Please help.
Upvotes: 4
Views: 214
Reputation: 174957
Here's my solution:
<?php
//Initialization
$search = array('aa','bb','é');
$string = "Hello, this is a test paragraph aa 123 test bb 456";
//This will form (aa|bb|é), for the regex pattern
$search_string = "(".implode("|",$search).")";
//Replace "<any_search_word> <the_word_after_that>" with "<any_search_word>"
$string = preg_replace("/$search_string\s+(\S+)/","$1", $string);
var_dump($string);
You replace "SEARCH_WORD NEXT_WORD" with "SEARCH_WORD", thus eliminating "NEXT_WORD".
Upvotes: 2
Reputation: 42895
You can simply use phps preg_replace()
function for this:
#!/usr/bin/php
<?php
// the payload to process
$input = "Hello, this is a test paragraph aa 123 test bb 456 and so on.";
// initialization
$patterns = array();
$tokens = array('aa','bb','cc');
// setup matching patterns
foreach ($tokens as $token)
$patterns[] = sprintf('/%s\s([^\s]+)/i', $token);
// replacement stage
$output = preg_replace ( $patterns, '', $input );
// debug output
echo "input: ".$input."\n";
echo "output:".$output."\n";
?>
Upvotes: 0