Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regex is probably <em>one</em> way:</p> <pre><code>$src = preg_replace('(.*?(?&lt;==)([^&amp;"]+).*)i', '\\1', $text); </code></pre> <p>However I would give the following hint as <em>the way</em>: Divide an conquer. Divide a problem into smaller ones and solve the overall problem step-by-step then. This works for many problems. As an example:</p> <ul> <li>First: Get the SRC attribute value from the string</li> </ul> <p>There are a thousand ways to do this, incl. regular expressions. As a regex would assume the string is always formatted this way and extracting the URL attribute value is actually trivial, I am using a different function that supports regular expressions: <a href="http://php.net/sscanf" rel="nofollow"><code>sscanf</code></a>:</p> <pre><code>$url = sscanf($text, '&lt;iframe src="%[^"]')[0]; # string(126) "http://www.google.com/calendar/embed?src=canaca.../Tokyo" </code></pre> <p>So now the URL is already extracted. As this is an URL, it can be processed with standard URL functions. Let's see:</p> <ul> <li>Second: Parse the query from the URL</li> </ul> <p>To get the SRC value from the URL you could use a regular expression again. However, as PHP has functions that are specific to URL handling, I use those instead. I can exactly say what I need with <a href="http://php.net/parse_url" rel="nofollow"><code>parse_url</code></a>. And this time I first of all need the <em>query</em> part of the URL. That is the part that has the query variables after the question mark:</p> <pre><code>$query = parse_url($url, PHP_URL_QUERY); # string(89) "src=canacad.ac.jp_dqrg6k9pg1s879somecodekj88.../Tokyo" </code></pre> <p>This is already one step further to the value we're looking for. So there is another step to do:</p> <ul> <li>Third: Parse the SRC value from the query</li> </ul> <p>Here again PHP has a function built in to do that. We can extract all variables in a query from an URL with the <a href="http://php.net/parse_str" rel="nofollow"><code>parse_str</code></a> function. As it returns the results via a function parameter, this now needs two lines of code:</p> <pre><code>parse_str($query, $vars); $src = $vars['src']; # string(68) "canacad.ac.jp_dqrg6k9pg1s879somecodekj88c8@group.calendar.google.com" </code></pre> <p>And now in the <code>$src</code> variable is the value you're looking for.</p> <p>Here the whole code from above at a glance:</p> <pre><code>$text = '&lt;iframe src="http://www.google.com/calendar/embed?src=canacad.ac.jp_dqrg6k9pg1s879somecodekj88c8%40group.calendar.google.com&amp;ctz=Asia/Tokyo" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;'; $url = sscanf($text, '&lt;iframe src="%[^"]')[0]; $query = parse_url($url, PHP_URL_QUERY); parse_str($query, $vars); $src = $vars['src']; var_dump($url, $query, $src); </code></pre> <p>The output is as follows, showing all three steps:</p> <pre><code>string(126) "http://www.google.com/calendar/embed?src=canacad.ac.jp_dqrg6k9pg1s879somecodekj88c8%40group.calendar.google.com&amp;ctz=Asia/Tokyo" string(89) "src=canacad.ac.jp_dqrg6k9pg1s879somecodekj88c8%40group.calendar.google.com&amp;ctz=Asia/Tokyo" string(68) "canacad.ac.jp_dqrg6k9pg1s879somecodekj88c8@group.calendar.google.com" </code></pre> <p>So regardless which functions you use in each of those steps: if you divide a problem into smaller parts you nearly always will be able to solve larger problems. And also if there is a problem in one of the sub-steps, you only need to fix a single step - not the whole operation. If you use a single regular expression to do all this work, you would have the single point of failure (and crafting a good regular expression in the world of HTML and URLs is non-trivial so it likely will break).</p> <p>A perfect solution would use an HTML parser for the first step for example. For example with the <a href="http://php.net/book.tidy" rel="nofollow"><em>Tidy extension</em></a> or with the popular <em>DOMDocument extension</em>:</p> <pre><code>// Tidy (non error-checked): $url = tidy_parse_string($text)-&gt;body()-&gt;child[0]-&gt;attribute['src']; // DOMDocument (non error-checked): $url = @DOMDocument::loadHTML($text)-&gt;getElementsByTagname('iframe') -&gt;item(0)-&gt;getAttribute('src'); </code></pre> <p>A HTML parser has the benefit that it understand the HTML elements. You can look for specific elements and attributes even if their position changes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload