Emil Devantie Brockdorff
Emil Devantie Brockdorff

Reputation: 4954

Parse string to get URL within

I’m having a string which is autogenerated by a third party web-service, where I want to get the URL from the string. The string looks like this:

'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");'

I want to retrieve the URL, in above case this URL: http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\ and I want to remove escape backslashes so the URL is: http://us2.campaign-archive1.com/?u=fdf89fgd7sdf7d8&id=ffd89dfef3/

I’ve tried with some different parsers and Regex, but I’m not that strong in Regex and can’t seem to get the URL correctly. I tried this preg_match but it doesn’t work and is only returning empty arrays:

%^((http?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i

Any help is much appreciated.

Sincere
- Mestika

Upvotes: 0

Views: 580

Answers (3)

Ayman Safadi
Ayman Safadi

Reputation: 11552

Try this:

<?php
$response = 'document.write("<div class=\"display_archive\"><div class=\"campaign\">20\/12\/2011 - <a href=\"http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3\" title=\"News\" target=\"_blank\">News<\/a><\/div><\/div>");';

preg_match('/href=\\\\\"([^\"]+)/', $response, $matches);

echo 'Raw URL: ' . $matches[1] . '<br />';
echo 'Clean URL: ' . stripslashes($matches[1]);
?>

Upvotes: 1

Robin Michael Poothurai
Robin Michael Poothurai

Reputation: 5664

have you tried str_replace(). e.g)

$url = "http:\/\/us2.campaign-archive1.com\/?u=fdf89fgd7sdf7d8&id=ffd89dfef3";
$url = str_replace('\\', '', $url);

Upvotes: 0

Massimiliano Arione
Massimiliano Arione

Reputation: 2466

Your regex is not working becaus of slashes. Just pass the string by stripslashes() and then apply regex

Upvotes: 0

Related Questions