Anas Zilali Anas Zilali - 4 months ago 10
PHP Question

Extract url with .swf in the end from string

I want to extract a url ending with .swf using php.

EXAMPLE

TEST TEST TEST http://website.com/example.swf


I want to get the url with the .swf

I used a function
preg_replace()


preg_replace('!http://[^?#]+\.(?:swf|SWF)!Ui','', $content);


but it extract the url + some words after the url

THANKS..

Answer

After some clarification on what you need i think this will help you.

preg_match_all('#(?P<url>https?://[^\s]+\.swf) (?P<description>[^\n]+)#uiD', $content, $results);
$data = [];
foreach($results['url'] as $key => $value) {
    $data[] = [
         'url' => $value,
         'description' => $results['description'][$key]
    ];
}

// Here you can put your code for saving the data you want.
// if you need to replace the content urls with the <object> tag:
$content = preg_replace('#(https?://[^\s]+\.swf)#uiD', '<object src="$1"></object>', $content);