doutuo7156 2016-06-01 11:27 采纳率: 100%
浏览 18
已采纳

查找并从字母A开头的字符串中删除

Suppose I have a string and want to remove all the words that begin with the letter a or A.:

$string = 'Aorem ipsum adolor sit amet, consectetur dipiscing aelit.';

That's how I was thinking:

$words = explode(' ', $string);
    foreach ($words as $wordA) {
    if ($wordA ***starts with letter*** 'a' OR 'A') {

How to write the statement if ($wordA starts with letter a OR A) and then to remove those words from the string (maybe something like $stringB = preg_replace($wordA, ' ', $string); )? Or do otherwise?

Thanks!

  • 写回答

1条回答 默认 最新

  • dsfs504545 2016-06-01 11:40
    关注

    Please use like

    $string = 'Aorem ipsum adolor sit amet, consectetur dipiscing aelit.';
    echo "<br>".preg_replace('/(\ba\w+\b|\bA\w+\b)/', '', $string);
    

    OUTPUT:

    ipsum sit , consectetur dipiscing .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?