0

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!

  • 1
    Okay, i'll take your word on this... But could you please edit your question and be clear about what is the input and what is the expected output ? – Enissay Dec 24 '13 at 02:07
  • Mh... I think you're right! :) I've just edited my question, sorry. – Felice Mifappo Dec 24 '13 at 09:56
  • Did you check my answer ? `I have to write "params" somewhere in the regex` what does that mean, do you want to capture it too ? – Enissay Dec 24 '13 at 11:25
  • It works, it's hidden, check the source code of the web page... – Enissay Dec 24 '13 at 11:32
  • Yep, now I understand your code... the quote (") is the final marker. However, it doesn't work: the output is blank. Now, I'm wondering if the problem is elsewhere... I'm sure it's not strip_tags's fault (it's the first thing I've checked), but this afternoon I will scan all the code. Thanks very much for your help. – Felice Mifappo Dec 24 '13 at 12:22
  • Again, the output is there... to see it check the html source code or use `echo htmlentities($ouptput);`... – Enissay Dec 24 '13 at 14:52

2 Answers2

0

Your regex will be \[soundcloud url="https://api\.soundcloud\.com/tracks/([^"]*?)"([^\]]*?)\]

and you can use preg_replace like this

$String = preg_replace('/\\[soundcloud url="https:\/\/api\\.soundcloud\\.com\/tracks\/([^"]*?)"([^\]]*?)\\]/',
    '<audio type="soundcloud">$\1</audio>')
sjagr
  • 15,983
  • 5
  • 40
  • 67
  • Mh... this doesn't work... "audio_id" should be ONLY what is between: [soundcloud url="https://api.soundcloud.com/tracks/" AND params="*] ...so I have to write "params" somewhere in the regex – Felice Mifappo Dec 24 '13 at 09:43
  • No you don't. This regex matches everything up to `...tracks/` and groups everything between that and the quotation marks as the `audio_id`. Then after the quotation marks anything that isn't a square bracket (`]`) is matched and tossed, including `params` and anything else that might follow the `url` attribute. Granted I haven't tested this `preg_replace` but you should read into regex to understand what I'm doing here. – sjagr Dec 24 '13 at 15:53
0

Regex

Well, you can use the following pattern wich extracts the audio_id, then replaces the whole input with <audio type="soundcloud">\1</audio> where \1 is the extracted audio_id:

/.*?tracks\/([^"]+).*/

Working DEMO

Explanation:

  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  tracks                   'tracks'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^"]+                    any character except: '"' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

PHP Code

<?
    $input = '[soundcloud url="https://api.soundcloud.com/tracks/audio_id" params="*]';

    $pattern = '/.*?tracks\/([^"]+).*/';

    $replacement= '<audio type="soundcloud">\1</audio>';

    $ouptput = preg_replace($pattern, $replacement, $input);

    echo htmlentities($ouptput);
?>

Live DEMO

HTML parsing

As a final word, I don't know if that's the only case you're using regex to parse an HTML code, but please consider using a more appropriate tool for that... There are many HTML parsers outthere (one of the best is PHP Simple HTML DOM Parser)...

If you still asking why, check HERE.

Community
  • 1
  • 1
Enissay
  • 4,969
  • 3
  • 29
  • 56