1

I get iframe from some api and i hold this iframe in some var.

I want to search for "height" and change his value to something else. the same with "scrolling".

For example:

<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>

After the php function the iframe will be:

we have change the "height" to 600px and "scrolling" to no

<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>

i have solution with this code:

$iframe = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"600"\2', $iframe);

the problem is that after the "preg_replace" run it remove all html attributes after the "height"

Thanks

7
  • You are better off doing this with java script. Commented Sep 18, 2015 at 9:07
  • Use javascript instead . . Commented Sep 18, 2015 at 9:07
  • Hi, i know i can do this in javascript the question is how can we do the same solution with php? like using "preg_replace" Commented Sep 18, 2015 at 9:10
  • 2
    Take a look at "regular expressions" and the functions php provides to use those. You are looking for preg_replace(): php.net/manual/en/function.preg-replace.php Commented Sep 18, 2015 at 9:14
  • 1
    There are about 65926593659 examples about that function alone here on SO. And about 5639465937 times more on google. And actually there are very good examples in the documentation of the function which is why I posted that link. You sure you need another one? Why? Sorry, don't get this wrong. But as a programmer you have to learn to look for a solution. You are expected to be able to find an example yourself. I am pretty sure you are able to find it. We gave you a hint what to look for exactly. Now it is up to you to use that hint. Commented Sep 18, 2015 at 9:16

2 Answers 2

3

You can use DOMDocument for it. Something like this:

function changeIframe($html) {

    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $iframes = $dom->getElementsByTagName('iframe');
    if (isset($iframes[0])) {
        $iframes[0]->setAttribute('height', '600');
        $iframes[0]->setAttribute('scrolling', 'no');
        return $dom->saveHTML($iframes[0]);
    } else {
        return false;
    }
}

$html = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';

echo changeIframe($html);

With this method you can modify iframe as you want.

Thanks.

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

Comments

1

An example as you requested:

$str = '<iframe src="someurl.com" width="540" height="450" scrolling="yes" style="border: none;"></iframe>';

$str = preg_replace('/height=[\"\'][0-9]+[\"\']/i', 'height="600"', $str);
$str = preg_replace('/scrolling=[\"\']yes[\"\']/i', 'scrolling="no"', $str);

echo $str; // -> '<iframe src="someurl.com" width="540" height="600" scrolling="no" style="border: none;"></iframe>'

5 Comments

HI, your example can be good if the height value is static but not for dynamic value.
This won't work if either single quotes are used (as in height='450'), the original height differs (height="768") or uppercase letters are used (e.g. HEIGHT="450").
I see your point @user2413244. I updated my answer to fit your needs
@feeela I just updated it with regex and I fixed the things you suggested. Thank you
@MarkNijboer I hoped you'll switch to proper methods for DOM manipulation. String replacement is going to fail sooner or later… See the answer of Taron Saribekyan for a sane and stable way of changing the DOM.

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.