0

The iframe is assigned to a variable $string. I want extract the texts between embed/ and ?rel in the iframes src. So i would be looking for this code sL-2oYAI35o

<iframe width="420" height="315" src="http://www.youtube.com/embed/sL-2oYAI35o?rel=0" frameborder="0" allowfullscreen></iframe>

Would I need to use a php substr function or regex? I cant use the regular substring because the start value may differ.

2
  • You should use regex. I don't see an embed or rel tag in the question. Can you post the whole string? Commented May 21, 2013 at 23:03
  • Sorry the embed is in the iframes src Commented May 21, 2013 at 23:05

2 Answers 2

1

I tested this and it works.

$string = preg_replace('/embed.*\?rel/', 'embed?rel', $string);

Edit thanks to @Orangepill

$string = preg_replace('/embed(.*)\?rel/', 'embed?rel', $string);

Edit 2 I tested it and it works identically with or without the capture group.

Sign up to request clarification or add additional context in comments.

4 Comments

Its removing the embed code alright but not assigning it to the new $string variable. How can I do this?
Did you try it with the most recent edit, where it begins with the variable assignment?
Preg_match with the same pattern but put a capture group around the .*
Works identically with or without capture group, I added it to edit though just inc ase others find that works for them.
0
$text = '<iframe width="420" height="315" src="http://www.youtube.com/embed/sL-2oYAI35o?rel=0" frameborder="0" allowfullscreen></iframe>';

preg_match('`embed/(.+)\?rel`i', $text, $matches);
$code = (!empty($matches[1])) ? $matches[1] : '';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.