0

特定の単語を削除しようとしています。

$data = str_replace( $wordsToRemove, '!', $data );

しかし、これは単語から特定の文字を残します。たとえば、testが削除する単語の場合、testing!ingになり、tests!sになります。

だから私はこれらのようなものを取り除こうとしています:

$data = preg_replace("/!anyAmountofCharsRemovedUntilSingleSpace /", ' ', $data);

これは正しい方法ですか?

4

2 に答える 2

1

これを試して:

<?php
$words = 'Hello there this is some sample text';
$replaced =  preg_replace('/th.*? /','',$words);
echo $replaced;
?>

出力:

こんにちは、サンプルテキストです

編集

<?php
$words = 'Hello there this is some sample text';
$chars = array(
    'the',
    'so',
    'sa'
);

for($i = 0; $i < count($chars); $i++)
    $words =  preg_replace('/'.$chars[$i].'.*? /','',$words);

echo $words;
?>

出力:

こんにちはこれはテキストです

于 2012-07-26T10:12:16.520 に答える
0

単語を削除したいのに、なぜそれらを「!」に置き換えるのですか?

あなたは単に書くことができます

$data = str_replace( $wordsToRemove, '', $data );

これは単語をnullに置き換えます。

于 2012-07-26T10:07:33.207 に答える