0

I'm trying to delete specific words.

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

But this leaves certain chars off words. So for example if test is a word to remove, testing is now !ing, and tests becomes !s

So I'm trying to get rid of these likes this:

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

Is this the right way to do it?

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 回答