dongshenling6585 2015-04-07 12:32
浏览 143

仅使用php删除第一行的单词

I have text:

I have new blue car
I have new red and blue cars

How remove what word i want from first line using php?

Ex:

        $text = preg_replace("/^(blue>){1}/", "", $text);

The result should be:

I have new car
I have new red and blue cars

And i want example for remove "p br " it is posible.

<p></p><br/>I have new blue car
I have new red and blue cars
  • 写回答

1条回答 默认 最新

  • douchi5822 2015-04-07 13:09
    关注

    The following will find the first row, replace the 'blue' word on that line with nothing (remove it), strip tags and remove leading/trailing space.

    • Will only remove whole words, e.g. not 'blue' in 'blues'
    • Will not remove word from following lines if not found in first line
    • Will not strip tags from following lines

    Code:

    $text = "<p></p><br/>I have new blue car
    I have new <b>red<b> and blue cars";
    $word = 'blue';
    
    $text = preg_replace_callback(
        '/.*$/m', // Match single line
        function ($matches) use ($word) {
            // Remove word (\b = word boundary), strip tags and trim off whitespace
            return trim(
                strip_tags(
                    preg_replace('/\b' . $word. '\s*\b/', '', $matches[0])
                )
            );
        },
        $text,
        1 // Match first line only
    );
    
    echo $text, PHP_EOL;
    

    Output:

    I have new car
    I have new <b>red<b> and blue cars
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)