Reputation: 172
So after a bit of stringy business, I was able to get a large chunk of substring from a haystack of string closer to my target after searching the first occurrence of "getflashmedia"
var_dump($str) =
string(1735) "getflashmedia" src="http://www.exampleURL.com/media-name.mp4"></object>
.../*a haystack of code as string*/"
I want to get the URL inside src
but this is varying in length, so I can't really use the substr()
function
Upvotes: 1
Views: 54
Reputation: 1356
I would strongly recommend rather than using string functions or regular expressions to parse XML/HTML, you should use XML parsers. You can build a much more reliable scraper this way.
XML parsers can handle situations you may not think of when writing your string handling code.
See XML Parser: http://php.net/manual/en/book.xml.php
Another option is SimpleXML: http://php.net/manual/en/simplexml.examples-basic.php
There are a number of libraries suitable for it.
Upvotes: 2
Reputation: 1253
Use a regular expression, I'd recommend the following: src="(.*?)"
.
This expression matches src="
literally, then starts capturing and stops when it finds another "
.
<?php
$input = 'getflashmedia" src="http://www.exampleURL.com/media-name.mp4"></object>';
preg_match_all('/src="(.*?)"/', $input, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => http://www.exampleURL.com/media-name.mp4
)
This will get every link, from every src attribute in the input string. If you only need the first, use preg_match()
.
Upvotes: 3