1

I have the following html string in my php code:

<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>

I want to append parameters to the URL give in the src attribute. The result should look as follows:

<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>

As you can see, in my example the string /APPENDAGE should be appended to the URL givein in the src attribute.

Usually I would do something like this with a string-replace method, however, in the given case this approach won't work. What would be an efficient approach to reach my goal?

7
  • What have you tried, can you share some of your work. Commented Oct 26, 2017 at 9:13
  • i have tried string replacement function, but it doesn't work due to the lack of "input string" that should be replaced because the video ID (e.g. 123456789 can be different each time) Commented Oct 26, 2017 at 9:14
  • 1
    I'd be loading your HTML up with DomDocument or SimpleXML and finding the nodes that way. Commented Oct 26, 2017 at 9:14
  • 2
    Where do you get the "iframe"-String from? If it's hardcoded in PHP you can simply add the value to it, also why is replace not working what have you tried? Commented Oct 26, 2017 at 9:14
  • it's not hardcoded. replace does not work because the id can change. Commented Oct 26, 2017 at 9:18

2 Answers 2

1

If you don't mind using regex, there is a simple solution for this: (DEMO)

$str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
';

$replaced = preg_replace('/(?<=src=")(.*?)(?=")/', '$1/APPENDAGE', $str);

var_dump($replaced);

Output:

string '<iframe src="https://player.vimeo.com/video/123456789/APPENDAGE" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
' (length=215)

This will simply match the url, insdie src attr and append /APPENDAGE to it, you can change it to whatever you want.

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

4 Comments

thanks. it will add /APPENDAGE and not just APPENDAGE right?
@beta Yes, as you can see '$1/APPENDAGE' there is a / between the $1 and APPENDAGE
yes, i know. but in your last sentences you only write APPENDAGE and not /APPENDAGE.
@beta Fixed it.
0

if you don't mind a large code

<?php
 $str = '<iframe src="https://player.vimeo.com/video/123456789" width="604" height="340" frameborder="0" title="Ovarian Psycos - Teaser" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>';

$obj = simplexml_load_string($str);
//print_r($obj);
$obj['src'] = $obj['src'].'/APPENDAGE';
$s = $obj->asXML();

print(substr( $s, strpos($s, "\n")+1 ));

But this code will also work for rest of the attributes

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.