Monday, 15 August 2011

php - regex to remove hashtag from string -



php - regex to remove hashtag from string -

i have next code regex trying remove hashtag string:

$text = "#jualan #jualanku #bag #bandung #jakarta #olshop #olshopindo #olshopindonesia #trusted #trustedseller #trustedolshop #tas #tasbagus #goodquality #premium #premiumquality #trixie #trixie handbag #baju #kaos #spandex #bestseller #trixiewardrobe \nbella top\n\nsize s m l\n\nidr 99.000\n\nline: merlinehadiwijaya"; $newcaption = preg_replace('/#(?=[\w-]+)/', '', preg_replace('/(?:#[\w-]+\s*)+$/', '', $text));

however removes '#' , not whole hashtag. doing wrong in regex?

i wanted result be:

\nbella top\n\nsize s m l\n\nidr 99.000\n\nline: merlinehadiwijaya

you first using positive lookahead assertion asserts specified characters follow preceding pound sign, reason pound sign beingness removed input string. secondly trying utilize cascading preg_replace() calls not necessary. in 1 phone call ...

$newcaption = preg_replace('/#\s+ */', '', $text);

note: have bag without preceding hashtag well, retained in result.

output

bag bella top size s m l idr 99.000 line: merlinehadiwijaya

code demo

php regex

No comments:

Post a Comment