I am trying to strip all non letters from a file path, but I need to leave the extension at the end.
File example: $text = cat.jpg
I am current using this $text = preg_replace('/[^\\\\pL\\d]+/u', '-', $text);
Result: cat-jpg
But this also converts any periods to a hyphen as well, I looked around and tried what I found from other posts, but they just removed the period all together.
Help would be appreciated.
You can use this regex based on alternation and negative lookahead for your search:
[^\pL\pN.]+|\.(?![^.]+$)
RegEx Breakup:
[^\pL\pN.]+ # Search 1 or more of any char that is not DOT and letter and number (unicode)
| # OR
\. # search for DOT
(?![^.]+$) # negative lookahead to skip DOT that is just before file extension
In PHP code:
$text = preg_replace('/[^\pL\pN.]+|\.(?![^.]+$)/u', '-', $text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.