4

検索文字列の次の単語を削除する必要があります.. array('aa','bb','é'); のような検索配列があります。

これは私の段落「こんにちは、これはテスト段落 aa 123 テスト bb 456 です」です。

この段落では、123 と 456 を削除する必要があります。

$pattern        = "/\bé\b/i";
$check_string       = preg_match($pattern,'Hello, this is a test paragraph aa 123 test é 456');

次の単語を取得する方法?? 助けてください。

4

2 に答える 2

2

これが私の解決策です:

<?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);

「SEARCH_WORD NEXT_WORD」を「SEARCH_WORD」に置き換えて、「NEXT_WORD」を削除します。

于 2012-11-10T09:16:15.940 に答える
0

これには単純に phpspreg_replace()関数を使用できます。

#!/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";

?>
于 2012-11-10T09:17:02.580 に答える