2016-03-07 91 views
0

我想刪除內容中以#開頭的單詞。php刪除單詞開頭#

例如

$content = "#test Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry #test2 standard dummy text ever since the 1500s, when an unknown printer took a galley of type #test3 and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. #test4 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum #test5." 

這裏#爲test1,#TEST2,TEST3#,#TEST4,#TEST5是#字。我想刪除那些單詞。我使用preg_replace。但它沒有得到輸出。

請幫幫我。謝謝

+0

發佈您用來嘗試此操作的代碼。 –

+0

這可能是非常接近,http://stackoverflow.com/questions/35711066/split-php-string-only-after-symbol/35711122#35711122(用'替換'交換'match_all'並交換'\ +'用'#'。如果沒有發佈你現在有的.. – chris85

回答

2

你可以試試/#[a-z0-9]+/i。此模式將替換任何字母數字字符,然後用空格替換#

preg_replace('/#[a-z0-9]+/i', '', $content) 
+0

不需要''''''應該加上'i'修飾符,或者加上大寫。 – chris85

+0

@ chris85謝謝哥們。更新。 –

+0

它didn 't替換最後一個字符串,即字符串「#EmpireKosher #Brooklyn #Supermarket #Kosher #WeeklySpecials #ShopKosher #KosherFood #CrownHeights」它沒有替換#CrownHeights因爲最後一個單詞不包含空格如何解決這個問題? –

0

你可以試試這個preg_replace('#.*?(?= |\.|,|!|\?)', '', $string);

它將刪除任何字符串開頭#