I need help for regex in php... "jolly char" in string replace. This is what I have now:
$String = str_replace('<audio type="soundcloud">'.
'[soundcloud url="https://api.soundcloud.com/tracks/',
'<audio type="soundcloud">', $String);
$String = str_replace(' params="color=ff6600&auto_play=false&'.
'show_artwork=true" width="100%" height="166" iframe="true" /]</audio>',
'</audio>', $String);
Input is:
[soundcloud url="https://api.soundcloud.com/tracks/audio_id" params="color=ff6600&auto_play=false&show_artwork=true" width="100%" height="166" iframe="true" /]
Output is:
<audio type="soundcloud">audio_id</audio>
and that's okay only if param
remains unchanged (I wish to match any param
value)
Now, I'm trying to solve it with regular expressions.
This is what I would like to replace:
[soundcloud url="https://api.soundcloud.com/tracks/audio_id" params="*]
...in order to have this:
<audio type="soundcloud">audio_id</audio>
(I don't need to save any parameters, for the moment)
So, I have to DELETE any char between params=" and ].
Can someone help me? I promise I will study, before next dumb question! :)
EDIT #1:
I apologize for my unclear question, and I try to explain it in another way. This is what I have in input:
[soundcloud url="https://api.soundcloud.com/tracks/123456789" params="color=ff6600&auto_play=false&show_artwork=true" width="100%" height="166" iframe="true" /]
This is what I need as output:
<audio type="soundcloud">123456789</audio>
...where:
- "123456789" changes and has to be saved.
- I have to del all text between "params" and "]" (included).
I think I need a FINAL marker, so I have to write "params" somewhere in the regex...
EDIT #2:
This is my last attempt:
$String = preg_replace('\<audio type="soundcloud"\>\[soundcloud url=\"https\:\/\/api.soundcloud.com\/tracks\/([^"]+).*.\<\/audio\>', '<audio type="soundcloud">\1</audio>', $String);
...it works in the regex101.com demo, but it doesn't in my page.
EDIT #3:
Fixed! Of course your answers were right... I got in trouble for two missing slashes in edit #2!
This is the right version:
$String = preg_replace('/\<audio type=\"soundcloud\"\>\[soundcloud url=\"https\:\/\/api.soundcloud.com\/tracks\/([^"]+).*.\<\/audio\>/', '<audio type="soundcloud">\1</audio>', $String);
Thanks very much for your help... and Happy New Year!