deleting a word starting with a specific letter/character php

后端 未结 4 1395
名媛妹妹
名媛妹妹 2021-01-28 21:43

here is a simple example:

$correction = \"game\";
$text =\"Hello this is an example of a $word that starts with a $dollar sign\";
$text = str_replace(\"\\$word\"         


        
相关标签:
4条回答
  • 2021-01-28 21:50

    Use a regular expression and preg_replace

    $correction = "game";
    $text ="Hello this is an example of a \$word that starts with a \$dollar sign";
    $text = preg_replace('/\$\w+/', $correction, $text);
    print $text;
    
    0 讨论(0)
  • 2021-01-28 21:56

    Something like so:

    preg_replace("/(\w*)$([a-z]+)/i", "$1$correction", $text);
    

    Note: I haven't tried it so my regex may be slightly flawed.

    0 讨论(0)
  • 2021-01-28 21:59

    You'll need a regex for this:

    $text = preg_replace( '/\$[a-z]+/i', 'game', $text);
    
    0 讨论(0)
  • 2021-01-28 22:02

    A simple regex should do the trick:

    $text = preg_replace("#\$[^\b\s]+#", $correction, $text);
    
    0 讨论(0)
提交回复
热议问题