my string may be like this:
@ *lorem.jpg,,, ip sum.jpg,dolor ..jpg,-/ ?
in fact – it is a dirty csv
string – having names of jpg images
I need to remove any
non-alphanum chars – from both sides
of the string
then – inside the resulting string – remove the same – except
commas and dots
then – remove duplicates commas and dots – if any – replace them with single ones
so the final result should be:
lorem.jpg,ipsum.jpg,dolor.jpg
I firstly tried to remove any white space – anywhere
$str = str_replace(" ", "", $str);
then I used various forms of trim
functions – but it is tedious and a lot of code
the additional problem is – duplicates commas and dots may have one
or more
instances – for example – ..
or ,,,,
is there a way to solve this using regex, pls ?
3
Answers
Can you try this :
Look at
https://www.php.net/manual/en/function.preg-replace.php
It replace anything inside a string based on pattern. s represent all space char, but care of NBSP (non breakable space, h match it )
Exemple 4
It will be something like that
List of modeled steps following your words:
Step 1
"remove any non-alphanum chars from both sides of the string"
translated: remove trailing and tailing consecutive [^a-zA-Z0-9] characters
regex: replace
^[^a-zA-Z0-9]*(.*?)[^a-zA-Z0-9]*$
with$1
Step 2
[^a-zA-Z0-9.,]
with empty stringStep 3
instance
(.{2,})
with.
(,{2,})
with,
PHP Demo:
https://onlinephp.io/c/512e1