PHP removes the word next to the search bar

I need to delete the next word of the search string .. I have a search array, for example 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.

+4
source share
2 answers

Here is 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", thereby eliminating "NEXT_WORD".

+2
source

You can simply use the phps preg_replace() function to do 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"; ?> 
0
source

Source: https://habr.com/ru/post/1445195/


All Articles